spring boot配置之@PropertySource、@ImportResource

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

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

  1、配置类==代替=>配置文件beans.xml

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

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

2、

resources下新建person.properties

person.last-name=李四
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps..k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

并注释掉application.properties中的person对象配置

修改Person.java

package com.cnstrong.springboot.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 将配置文件中配置的每一个属性的值映射到这个组件中
 * @ConfigurationProperties : 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
 *                  prefix = "",配置文件中哪个下面的所有属性进行一一映射,默认从全局配置文件中获取值
 */
@PropertySource(value = {"classpath:person.properties"})

//只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/从配置文件、环境变量中获取值${key}/Spring的表达式语言#{SpEL}"></property>
     * </bean>
     */
    //@Value("${person.last-name}")
    //@Email   //lastName必须是邮箱格式的
    private String lastName;

   //@Value("#{11*2}")
    private Integer age;

    //@Value("true")
    private Boolean boss;

    private Date birth;

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

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}

运行test包SpringBoot01HelloworldQuickApplicationTests类下的contextLoads方法

3、Spring Boot里面没有Spring的配置文件,自己编写的配置文件也不能自动识别;

     想让Spring的配置文件生效加载进来需要用@ImportResource标注在一个配置类上

新建service包HelloService类

package com.cnstrong.springboot.service;

public class HelloService {
}

resources目录下新建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.cnstrong.springboot.service.HelloService"></bean>
</beans>

修改SpringBoot01HelloworldQuickApplicationTests测试类

package com.cnstrong.springboot;

import com.cnstrong.springboot.bean.Person;
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;

/**
 * Springboot单元测试
 * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot01HelloworldQuickApplicationTests {

    @Autowired
    Person person;

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(){
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}
运行testHelloService方法,发现

主类SpringBoot01HelloworldQuickApplication中加入@ImportResource配置

package com.cnstrong.springboot;

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

@ImportResource(locations = {"classpath:beans.xml"})   //导入Spring的配置文件让其生效
@SpringBootApplication
public class SpringBoot01HelloworldQuickApplication {

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

}
重新运行testHelloService方法

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

1、配置类==代替=>配置文件beans.xml

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

将配置类SpringBoot01HelloworldQuickApplication中的@ImportResource(locations = {"classpath:beans.xml"})注释掉

不编写spring的配置文件beans.xml

新建config包MyAppConfig类

package com.cnstrong.springboot.config;

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

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


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

运行测试类SpringBoot01HelloworldQuickApplicationTests的testHelloService方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值