二-Spring常用配置(2)-Bean生命、Profile-【JavaEE开发的颠覆者】

98 阅读2分钟

一、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) {
        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();
    }
}

这里写图片描述