Spring Boot @PropertySource&@ImportResource&@Bean注解

一.@PropertySource:加载指定的配置文件

例如:加载person.properties

person.properties内容:

person.last-name=李四
person.age=18
person.birth=2019/01/07
person.boss=true
person.maps.k1=v1
person.maps.k2=15
person.lists=a,b,c
person.dog.name=小狗
person.dog.age=5

测试加载相关代码Person实体类

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
    ...
}

运行单元测试代码

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
    @Autowired
    Person person;
    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

运行结果:

二.@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

例如:加载beans.xml

beans.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloService" class="com.hq.springboot.service.HelloService"></bean>
</beans>

在启动方法中加入自定义配置

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

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

}

 启动单元测试方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
    @Autowired
    Person person;
    @Autowired
    ApplicationContext ioc;
    @Test
    public void testHelloService(){
        boolean helloService = ioc.containsBean("helloService");
        System.out.println(helloService);
    }
    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

 打印结果:注入成功

 三.@bean

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

1、配置类@Configuration------>Spring配置文件

2、使用@Bean给容器中添加组件

例如加载配置类MyAppConfig.java

MyAppConfig.java内容

/**指明当前类是一个配置类:替代之前的Spring配置文件
 * 在配置文件中用<bean><bean/>标签添加组件
 * @author hq.zheng
 * @create 2019-01-08-下午 10:38
 */
@Configuration
public class MyAppConfig {
    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }
}

运行单元测试方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
    @Autowired
    Person person;
    @Autowired
    ApplicationContext ioc;
    @Test
    public void testHelloService(){
        boolean helloService = ioc.containsBean("helloService");
        System.out.println(helloService);
    }
    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

运行结果:

猜你喜欢

转载自blog.csdn.net/xm393392625/article/details/86102273