Spingboot|第六篇学习@PropertySource&@ImportResource&@Bean

Springboot 是简化Spring应用开发的创建、运行、调试、部署等一系列问题的框架是J2EE开发的一站式解决方案,自动装配的特性可以让我们更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻松的搭建出一个WEB工程
[如果你觉得对你有帮助,欢迎转发分享给更多的人学习]

上篇学习Spring Boot的配置文件分析@Value获取值@ConfigurationProperties获取值比较,接下来是学习@PropertySource&@ImportResource&@Bean

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

之前使用@ConfigurationProperties(prefix = “person”)给Person注入属性值,@ConfigurationProperties默认从全局配置文件中获取值,如何我们后来都把所有的配置信息都从全局的配置文件中获取,那么这个文件就写的太乱了。可以在resource下新建一个person.properties文件

person.last-name=九月
person.age=18
person.boss=true
person.birth=2018/10/11
person.maps.k1=v1
person.maps.k2=v2
person.lists=1,2,3
person.dog.name=dog
person.dog.age=3

使用@PropertySource(value = “{classpath:person.properties}”)获取指定文件的配置信息

@Component
@PropertySource(value = {"classpath:person.properties"})
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;
    //省略get/set
}

通过测试代码看到可以获取到指定文件的信息完成属性映射

Person{lastName='九月', age=18, boss=true, birth=Thu Oct 11 00:00:00 CST 2018, maps={k1=v1, k2=v2}, lists=[1, 2, 3], dog=Dog{name='dog', age=3}}

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

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

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

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

可以在resource下新建一个beams.xml文件,是不是想到了之前学习SSH的配置

<?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.example.demo.com.jiuyue.service.HelloService"></bean>
</beans>

想让Spring的配置文件生效,加载进来;将@ImportResource标注在一个配置类上就可以了
下面通过单元测试看是否将helloService注入到IOC容器中

@Autowired
private ApplicationContext ioc;
@Test
public void testHelloService(){
    //测试判断helloService是否在IOC容器中
    boolean b = ioc.containsBean("helloService");
    System.out.println(b);
}

Spingboot|第六篇学习@PropertySource&@ImportResource&@Bean

@bean使用全注解的方式,给容器中添加组件

以前我们是在标签中添加组件的,SpringBoot推荐给容器中添加组件使用全注解的方式
1、建一个配置类MyAppConfig,使用@Configuration注解这个配置类,相当于说明这个MyAppConfig是一个Sping配置文件
在配置类中使用@Bean注解方法,就是给容器添加组件,将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名

/**
 * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
 *
 * 在配置文件中用<bean><bean/>标签添加组件
 *
 */
@Configuration
public class MyAppConfig {

    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService02(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }
}

单元测试:
Spingboot|第六篇学习@PropertySource&@ImportResource&@Bean

说点什么

QQ学习交流群:277300227
微信公众号(欢迎关注):SeptemberNotes
Spingboot|第六篇学习@PropertySource&@ImportResource&@Bean