码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)

时间:2017-09-11 15:09:33      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:ons   技术   dispatch   idt   group   org   常用   java配置   files   

一、Bean的初始化和销毁

  在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持。在使用Java配置和注解配置下提供如下两种方式:

  (1)Java配置的方式:使用 @Bean 的 initMethod 和 destroyMethod(相当于xml配置中的 init-method 和 destroy-method)。

  (2)注解方式:利用JSR-250的 @PostContruct 和 @PreDestroy。

演示:

  1.增加 JSR-250 支持。

<!-- JSR-250 支持 -->
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>jsr250-api</artifactId>
      <version>1.0</version>
    </dependency>

  2.使用 @Bean 形式的Bean。

package com.ecworking.bean;

public class BeanWayService {

    public void init(){
        System.out.println("@Bean-init-method");
    }

    public BeanWayService() {
        super();
        System.out.println("初始化构造函数-BeanWayService");

    }

    private void destroy(){
        System.out.println("@Bean-destroy-method");
    }
}

  3.使用JSR250形式的Bean。

package com.ecworking.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class JSR250WayService {

    @PostConstruct // 在构造函数执行完之后执行
    public void init(){
        System.out.println("@JSR250-init-method");
    }

    public JSR250WayService() {
        super();
        System.out.println("初始化构造函数-JSR250WayService");
    }

    @PreDestroy // 在Bean销毁之前执行
    private void destroy(){
        System.out.println("@JSR250-destroy-method");
    }
}

  4.配置类。

package com.ecworking.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.ecworking.bean")
public class PrePostConfig {

    // initMethod 和 destroyMethod 指定BeanWayService类的 init 和 destroy 方法在构造之后、Bean销毁之前执行
    @Bean(initMethod = "init", destroyMethod = "destroy")
    BeanWayService beanWayService(){
        return new BeanWayService();
    }

    @Bean
    JSR250WayService jsr250WayService(){
        return new JSR250WayService();
    }

}

  5.运行。

package com.ecworking.bean;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class);

        BeanWayService beanWayService = context.getBean(BeanWayService.class);

        JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);

        context.close();
    }
}

运行结果:

技术分享

二、Profile

  Prifile为不同环境下提供不同不同配置提供了支持(开发环境和生产环境下的配置肯定是不同的,例如,数据库的配置)。

  1.通过设定Environment的 ActiveProfile来设定当前context需要使用的配置环境。在开发环境中使用@Profile注解类或方法,达到在不同环境下选择实例化不同的Bean。

  2.通过设定jvm的spring.profile.active参数来设置配置环境。

  3.Web项目设置在Servlet的context parameter中。

  Servlet2.5及以下:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>production</param-value>
    </init-param>
</servlet>

  Servlet3.0及以上

 

Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)

标签:ons   技术   dispatch   idt   group   org   常用   java配置   files   

原文地址:http://www.cnblogs.com/dyppp/p/7504794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!