版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/yu0_zhang0/article/details/83755394
1 概述
我们对于一些属性的配置肯定不会写在一个配置文件中这样显得耦合性太强,如何去指定配置文件呢? 下面我们就通过@PropertySource、@ImportResource、@Bean的使用这几个属性的学习来解决此问题。
2 @PropertySource
@PropertySource:加载指定的配置文件
- person 类
package cn.zhangyu.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.List;
import java.util.Map;
/**
* Created by grace on 2018/11/5.
*/
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
@PropertySource(value = {"classpath:person.properties"})
public class Person {
//人的属性
//@Email
private String myName;
private int age;
private String sex;
//map
private Map<String , Object> maps;
//list
private List<Dog> list;
private Dog dog;
public String getMyName() {
return myName;
}
public void setMyName(String myName) {
this.myName = myName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Dog> getList() {
return list;
}
public void setList(List<Dog> list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"myName='" + myName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", maps=" + maps +
", list=" + list +
", dog=" + dog +
'}';
}
}
- person.properties
person.myName=小明
person.age=20
person.sex=male
person.maps.k1=v1
person.maps.k2=v2
person.list[0].name=小顾
person.list[0].age=22
person.list[1].name=旺财
person.list[1].age=10
person.dog.name=旺旺
person.dog.age=30
3 @ImportResource
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
下面通过一个简单的demo来学习:
- 创建一个HelloService
public class HelloService {
}
- 创建一个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="cn.zhangyu.service.HelloService"></bean>
</beans>
- 在测试类中看看能不能获取注入到bean中的helloService
public class Springboot1ApplicationTests {
@Autowired
ApplicationContext ioc;
@Test
public void helloService(){
boolean helloSerice = ioc.containsBean("helloService");
System.out.println(helloSerice);
}
}
输出: false
从结果看并没有注入成功,那么我们加上@ImportResource
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class Springboot1Application {
public static void main(String[] args) {
SpringApplication.run(Springboot1Application.class, args);
}
}
结果:true 注入成功
但是这种方式肯定不好,有多个xml就要添加多个比较繁琐,所以,SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式:
1、配置类**@Configuration**------>Spring配置文件
2、使用**@Bean**给容器中添加组件
- 自定义配置类MyAppConfig
package cn.zhangyu;
import cn.zhangyu.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by grace on 2018/11/5.
*/
/**
* @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
*
* 在配置文件中用<bean><bean/>标签添加组件
*
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}
通过上面的测试类进行测试,
结果:
配置类@Bean给容器中添加组件了...
true
这种方式是推荐的。