Spring 全注解配置 bean 和 调用 (7) properties文件注入
2016-11-12 17:19
627 查看
package com.xiuye.config; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.Scope; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import com.xiuye.bean.Car; import com.xiuye.bean.Student; import com.xiuye.bean.WhiteCar; import com.xiuye.component.ComponentForStudent2; import com.xiuye.config.condition.StudentCondition; @Configuration @ComponentScan("com.xiuye.component") @Profile("dev") @PropertySource("test.properties") public class BeanConfiguration1 { //启用el表达式 ("${express}")解析功能,否则原样子语句注入 @Bean public PropertySourcesPlaceholderConfigurer pspc(){ return new PropertySourcesPlaceholderConfigurer(); } @Bean @Conditional(StudentCondition.class) public Student student() { return new Student("xiuye", "man", 18, 99); } /** * @param name * @param sex * @param age * @param level * @return */ @Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 必须原生,否则no longer has // any effect public Student student(String name, String sex, int age, int level) { return new Student(name, sex, age, level); } @Bean public ComponentForStudent2 cfs2(Student s) { ComponentForStudent2 cfs2 = new ComponentForStudent2(); cfs2.setStudent(s); return cfs2; } @Bean public Car car(){ return new Car(); } @Bean public WhiteCar wCar(){ return new WhiteCar(); } }
key=value programminglang=java,c\\c++,python,js,html,json,go,css
package com.xiuye.test; import javax.annotation.Resource; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.xiuye.bean.Car; import com.xiuye.bean.WhiteCar; import com.xiuye.component.ComponentForStudent; import com.xiuye.component.ComponentForStudent2; import com.xiuye.config.BeanConfiguration1; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = BeanConfiguration1.class) @ActiveProfiles("dev") public class TestMain { @Resource private ComponentForStudent cfs; @Resource private ComponentForStudent2 cfs2; @Resource private Car car; @Resource private Environment env; @Resource private WhiteCar wcar; @BeforeClass public static void envConfig() { System.setProperty("test", "true"); } @Test public void testCfs() { cfs.studentInfo(); } @Test public void testCfs2() { cfs2.studentInfo(); } @Test public void testCar() { this.car.configInfo(); } @Test public void testWhiteCar() { this.wcar.configInfo(); } //必须用"$" 而不是"#" @Value("${key}") private String value; @Value("${programminglang}") private String langs; @Test public void testPropertiesFile() { System.out.println(env.getProperty("key")); System.out.println(env.getProperty("programminglang")); System.out.println(value); System.out.println(langs); } }
十一月 12, 2016 5:15:27 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames 信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 十一月 12, 2016 5:15:27 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext] 十一月 12, 2016 5:15:27 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners 信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@163425a, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@d64342, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1ba5a14, org.springframework.test.context.transaction.TransactionalTestExecutionListener@11baa65, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@f438e] 十一月 12, 2016 5:15:27 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@9f9fb0: startup date [Sat Nov 12 17:15:27 CST 2016]; root of context hierarchy Active profile := dev test := true 十一月 12, 2016 5:15:27 下午 org.springframework.context.annotation.ConfigurationClassEnhancer intercept 警告: @Bean method BeanConfiguration1.pspc is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details. value java,c\c++,python,js,html,json,go,css value java,c\c++,python,js,html,json,go,css
相关文章推荐
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- spring注解注入properties配置文件
- Spring 中注入bean的properties配置文件位置问题解决
- Spring 中注入bean的properties配置文件位置问题解决
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得Appli
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- 开涛spring3(12.2) - 零配置 之 12.2 注解实现Bean依赖注入
- spring配置文件中bean标签中init-method和destroy-method和用注解方式配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- 模拟Spring注入和读取配置文件信息(支持bean 书写顺序随意)
- 【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- 【第十二章】零配置 之 12.1 概述 12.2 注解实现Bean依赖注入 ——跟我学spring3
- struts中的Action层调用服务层时,不需要在Spring配置文件中做属性注入