二-Spring常用配置(2)-Bean生命、Profile-【JavaEE开发的颠覆者】
2018-02-12 19:54
543 查看
一、Bean的初始化和销毁
在我们实际开发的时候,经常会遇到Bean在使用之前或者之后做某些必要的操作,Spring对Bean的生命周期的操作提供了支持。在使用Java配置和注解配置下提供如下两种方法。(1)Java配置:使用@Bean的initMethod和destroyMethod(相当于xml配置的init-method和destroy-method)
(2)注解方式:利用JSR-250的@PostConstruct和@PreDestroy
1.演示
(1)增加JSR250支持
<!--增加JSR250支持--> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
(2)使用@Bean形式的Bean
public class BeanWayService { public void init(){ System.out.println("@Bean--init-method"); } public BeanWayService() { System.out.println("初始化构造函数--BeanWayService"); } public void destroy(){ System.out.println("@Bean--destroy-method"); } }
(3)使用JSR250形式的Bean
@Service public class JSR250WayService { @PostConstruct public void init(){ System.out.println("JSR250--init-method"); } public JSR250WayService() { System.out.println("初始化构造函数--JSR250WayService"); } @PreDestroy public void destroy(){ System.out.println("JSR250--destroy-method"); } } /** * @PostConstruct 在构造函数执行完之后执行 * @PreDestroy 在Bean销毁之前执行 */
(4)配置类
//配置类 @Configuration @ComponentScan("com.zhao.spring.BeanInitDestroy") public class BeanInitDestroyConfig { @Bean(initMethod = "init",destroyMethod = "destroy") BeanWayService beanWayService(){ return new BeanWayService(); } } /** * initMethod和destroyMethod指定BeanWayService类的init和destroy方法在构造之后、Bean销毁之前执行 */
(5)启动类
public class BeanInitDestroyMain { public static void main(String[] args) { AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(BeanInitDestroyConfig.class); BeanWayService beanWayService=context.getBean(BeanWayService.class); JSR250WayService jsr250WayService=context.getBean(JSR250WayService.class); context.close(); } }
二、Profile
Profile为在不同的环境下使用不同的配置提供了支持。(1)通过设定Environment的ActiveProfiles来设定当前context需要使用的配置环境。在开发中使用@Profile注解类或者方法,达到不同情况下选择实例化不同的Bean。
(2)通过设定jvm的spring.profiles.active参数来设置配置环境。
(3)Web项目设置在Servlet的context parmeter中:
1.示例
(1)示例Bean
//示例Bean public class DemoBean { private String content; public DemoBean(String content) { this.content = content; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
(2)配置类
//配置类 @Configuration public class ProfileConfig { @Bean @Profile("dev") public DemoBean devdemoBean(){ return new DemoBean("from development profile"); } @Bean @Profile("prod") public DemoBean proddemoBean(){ return new DemoBean("from production profile"); } } /** * Profile为dev时实例化devdemoBean * Profile为prod时实例化proddemoBean */
(3)启动类
public class ProfileMain { public static void main(String[] args) { b4a0 AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(); //将活动的Profile设置为prod context.getEnvironment().setActiveProfiles("prod"); //后置注册Bean配置类,不然会报Bean未定义的错误 context.register(ProfileConfig.class); //刷新容器 context.refresh(); DemoBean demoBean=context.getBean(DemoBean.class); System.out.println(demoBean.getContent()); context.close(); } }
相关文章推荐
- 二-Spring常用配置(1)-Scope、EL-【JavaEE开发的颠覆者】
- 一-Spring基础(1)-配置-【JavaEE开发的颠覆者】
- SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 04-SpringBoot——Spring常用配置-Bean的Scope
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- Spring开发--Bean配置实例讲解
- Spring Bean配置默认为单实例 pring Bean生命
- 【Spring】Spring常用配置-Profile
- spring容器内建的profile功能实现开发环境、测试环境、生产环境配置自动切换
- Spring的开发配置与Spring中bean的实例化
- Spring开发--Bean配置实例讲解
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 「一句话经典」从Spring到SpringMVC再到JavaEE开发颠覆者Spring Boot,最后佛法无边Spring Cloud;只不过却成了nodejs的笑柄
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- Spring.profile实现开发、测试和生产环境的配置和切换
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换