天天看点

SpringBoot项目中的@PropertySource、@ImportResource、@Bean的用途

@PropertySource 加载指定配置文件

application.properties或application.yml是SpringBoot的默认配置文件,会被自动加载。假如我们现在有一个src/main/resources/person.properties:

person.lastName=Jhon
person.age=18
person.boss=false
person.birth=2020/03/21
person.maps.k1=v1
person.maps.k2=v2
person.lists=Tom,Sam
person.dog.name=small dog
person.dog.age=2      

首先这个自定义的配置文件不会被SpringBoot加载,那么我们就无法与我们的配置类Person.java做映射,所以我们可以在配置类上加下@PropertySource注解来实现加载:

package com.wong.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 将配置文件中配置的每一个属性值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
 *       prefix = "person":配置文件中哪个下面的所有属性值进行一一映射
 *   注意:只有这个组件是容器的组件,才能使用容器提供的ConfigurationProperties功能
 * @PropertySource加载指定的配置文件
 */
@Configuration
@PropertySource("classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Person{
  private String lastName;
  private int age;
  private boolean boss;
  private Date birth;

  private Map<String,Object> maps;
  private List<Object> lists;
  private Dog dog;

  // omit setter getter
}      

@ImportResource专门用来加载Spring的配置文件

1、定义组件HelloService

package com.wong.service;

public class HelloService {
}      

2、创建Spring配置文件src/main/resources/beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloService" class="com.wong.service.HelloService"/>
</beans>      

id的值就是组件在加入Spring容器后的唯一标识。

3、在配置类(有@Configuration注解的类)上加入此注解,以便在启动时,可以被加载,为了方便,现将其放到启动类上

package com.wong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class MainApplication{

        public static void main(String[] args){
                SpringApplication.run(MainApplication.class,args);
        }
}      

4、运行测试类src/test/java/com/wong/SpringBootConfigTests.java

package com.wong;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootConfigTests {

    @Autowired
    private ApplicationContext ioc;
    @Test
    public void testBeans(){
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);// true
    }

}      

@Bean给容器中添加组件

SpringBoot推荐使用全注解的方式给容器中添加组件:

  • 配置类即Spring配置文件
  • 使用@Bean给容器中添加组件

    所以可以将上面使用@ImportResource加载Spring配置文件的方式换成以下的方式来实现:

package com.wong.config;

import com.wong.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public HelloService myHelloService(){
        return new HelloService();
    }
}      
package com.wong;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootConfigTests {

    @Autowired
    private ApplicationContext ioc;

    @Test
    public void testMyHelloService(){
        boolean b = ioc.containsBean("myHelloService");
        System.out.println(b); // true
    }

}