Spring 全注解配置 bean 和 调用 (2) @Profile 自适应不同的模式或环境中
2016-11-12 12:10
615 查看
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.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Scope; import com.xiuye.bean.Student; import com.xiuye.component.ComponentForStudent2; @Configuration @ComponentScan("com.xiuye.component") @Profile("dev") public class BeanConfiguration1 { @Bean 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; } }
package com.xiuye.main; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.xiuye.bean.Student; import com.xiuye.component.Component; import com.xiuye.component.ComponentForStudent; import com.xiuye.config.BeanConfiguration1; public class Main { public static void main(String []args) { System.setProperty("spring.profiles.active","dev");//设置(开启)激活的开发环境 可以 不设置报错 //System.setProperty("spring.profiles.default","dev");//设置默认的开发环境 可以 ApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfiguration1.class); Student s = ac.getBean(Student.class); System.out.println(s); s = ac.getBean(Student.class, "Linda","Woman",22,70); System.out.println(s); ComponentForStudent cf = ac.getBean(ComponentForStudent.class); cf.studentInfo(); Component c = ac.getBean(Component.class, "100","200"); System.out.println(c); } }
package com.xiuye.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 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; @Test public void testCfs(){ cfs.studentInfo(); } @Test public void testCfs2(){ cfs2.studentInfo(); } }
结果:
十一月 12, 2016 12:04:38 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1af6ecc: startup date [Sat Nov 12 12:04:38 CST 2016]; root of context hierarchy
Student [name=xiuye, sex=man, age=18, level=99]
Student [name=Linda, sex=Woman, age=22, level=70]
Student [name=xiuye, sex=man, age=18, level=99]
Component [_1=100, _2=200]
十一月 12, 2016 12:10:19 下午 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 12:10:19 下午 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 12:10:19 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners 信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@75222b, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@203ea1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7f674d, org.springframework.test.context.transaction.TransactionalTestExecutionListener@1251891, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@140564] 十一月 12, 2016 12:10:19 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@109b8da: startup date [Sat Nov 12 12:10:19 CST 2016]; root of context hierarchy Student [name=xiuye, sex=man, age=18, level=99] Student [name=xiuye, sex=man, age=18, level=99] 十一月 12, 2016 12:10:20 下午 org.springframework.context.support.GenericApplicationContext doClose 信息: Closing org.springframework.context.support.GenericApplicationContext@109b8da: startup date [Sat Nov 12 12:10:19 CST 2016]; root of context hierarchy
相关文章推荐
- Java类中@Profile注解 springboot切换不同环境配置
- spring3.1 profile 配置不同的环境
- spring3.1 profile 配置不同的环境
- Spring 全注解配置 bean 和 调用 (8) @Aspect注解不生效解决办法 和 no-static 的警告解决办法
- Spring 全注解配置 bean 和 调用 (6) 自定义注解 自动装配歧义性消除
- springboot + profile(不同环境读取不同配置)
- spring3.1 profile 配置不同的环境
- Spring 全注解配置 bean 和 调用 (9) 5种通知
- 详解springboot + profile(不同环境读取不同配置)
- [置顶] spring3.1 profile 配置不同的环境
- 第十四章 springboot + profile(不同环境读取不同配置)
- 160922、配置:spring通过profile或@profile配置不同的环境(测试、开发、生产)
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Spring 全注解配置 bean 和 调用 (4) @Primary 自动装配歧义性
- Spring 全注解配置 bean 和 调用 (7) properties文件注入
- 在不同环境下调用Spring来管理的Bean.
- Spring 全注解配置 bean 和 调用 (5) @Qualifier 自动装配歧义性消除
- Spring-Boot使用profile来配置不同环境的配置文件
- 第十四章 springboot + profile(不同环境读取不同配置)
- 【第十四章】 springboot + profile(不同环境读取不同配置)