天天看点

飞5的Spring Boot2(6)- 导入外部配置

飞5的Spring Boot2(6)- 导入外部配置

@Bean下管理bean的生命周期

可以使用基于 Java 的配置来管理 bean 的生命周期。@Bean 支持两种属性,即 initMethod 和destroyMethod,这些属性可用于定义生命周期方法。在实例化 bean 或即将销毁它时,容器便可调用生命周期方法。生命周期方法也称为回调方法,因为它将由容器调用。使用 @Bean 注释注册的 bean 也支持 JSR-250 规定的标准 @PostConstruct 和 @PreDestroy 注释。如果您正在使用 XML 方法来定义 bean,那么就应该使用 bean 元素来定义生命周期回调方法。以下代码显示了在 XML 配置中通常使用 bean 元素定义回调的方法。

新建测试类TestConfiguration

1@Configuration
 2@ComponentScan(basePackages = "com.cloud.skyme")
 3public class TestConfiguration {
 4    public TestConfiguration() {
 5        System.out.println("TestConfiguration容器启动初始化。。。");
 6    }
 7    // @Bean注解注册bean,同时可以指定初始化和销毁方法
 8    @Bean(name = "testBean", initMethod = "start", destroyMethod = "cleanUp")
 9    @Scope("prototype")
10    public TestBean testBean() {
11        return new TestBean();
12    }
13}      

编写测试类,代码如下:

1public class TestMain {
 2    public static void main(String[] args) {
 3        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
 4        TestBean tb = (TestBean) context.getBean("testBean");
 5        tb.sayHello();
 6        System.out.println(tb);
 7        TestBean tb2 = (TestBean) context.getBean("testBean");
 8        tb2.sayHello();
 9        System.out.println(tb2);
10    }
11}      

运行得到结果输出:

115:00:14.503 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking init method  'start' on bean with name 'testBean'
 2TestBean 初始化。。。
 315:00:14.503 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'testBean'
 4TestBean sayHello...
 5username:null,url:null,password:null
 615:00:14.504 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'testBean'
 715:00:14.504 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'testConfiguration'
 815:00:14.504 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking init method  'start' on bean with name 'testBean'
 9TestBean 初始化。。。
1015:00:14.504 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'testBean'
11TestBean sayHello...
12username:null,url:null,password:null      

导入xml配置

在一些老的工程中,很多配置都是基于xml的方式进行配置的,那么有没有什么好的办法使用这些文件呢?

使用@ImportResource和@Value进行资源文件读取。

来看一个示例:

1@Configuration
 2@ImportResource("classpath:/com/cloud/skyme/properties-config.xml")
 3public class AppConfig{
 4    @Value("${jdbc.url}")
 5    private String url;
 6    @Value("${jdbc.username}")
 7    private String username;
 8    @Value("${jdbc.password}")
 9    private String password;
10    @Bean
11    public DataSource dataSource() {
12        return new DriverManagerDataSource(url,username,password);
13    }
14}      

使用@Configuration,把这个类作为一个配置来使用。@ImportResource就是引入一个这种资源,然后资源对应一个xml文件。

用@Value这个注解从资源文件中取出它的key赋值给成员变量,包括username、password等。然后再使用@Bean这个注解去创建DriverManagerDataSource的一个对象,也就是和第一种的方式一样,去创建这个Bean的对象,同时把url、username、password传入DriverManagerDataSource的构造器。

这样就达到了从资源文件中去加载资源文件的配置,并应用到bean的创建中。

1@ImportResource("classpath:config.xml")      

总结