5.SpringBoot导⼊配置@PropertySource、@ImportResour。。。
@PropertySource 、 @ImportResource 、@Bean  三个注解讲解:
@PropertySource
之前4-1、4-2、4-3 做实验的时候,把配置数据都写在了SpringBoot的根配置⽂件上,是全局的,那这样肯定是不⾏的,如果写很多个yml⽂件或properties⽂件也会很乱,也可能会重复。
@PropertySource:加载指定的配置⽂件;
新建⼀个s1.properties ⽂件
person.last-name=BiHu
person.age=18
person.boss=false
person.birth=2025/10/10
person.maps.k1=value1
person.maps.k2=value2
person.lists=v1,v2,v3,v4
person.dog.name=⼩勾勾
person.dog.age=2
s1.properties
然后JavaBean中指定⽤某个配置⽂件:
package com.bihu.Bean;
import org.straints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.t.properties.ConfigurationProperties;
import t.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@PropertySource("classpath:s1.properties")      //这个类指定⽤这个配置⽂件【classpath 这个不多说了】
@Component
@ConfigurationProperties(prefix = "person")
public class JavaBean {
private String lastName;
private Integer age;
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;
}
}
JavaBean.java
可以看到⾥⾯指定了⽤ classpath ⽬录下的s1.properties [classpath 是什么⾃⼰去百度]
注意事项:如果你SpringBoot 配置也有⼀样的 Person 数据,那么因为权限⼤范围⼤所以⽆论是否指定配置⽂件都⽤SpringBoot的,但⼀般不会那样做。⼀般SpringBoot配置⽂件都是配SpringBoot⾃⼰。
有了这个注解除了SpringBoot配置⽂件,其他的配置⽂件你都不⽤担⼼ "名字" 冲突。
就算s1 和 s2 ⼀样的配置都是 Person 都不怕,因为指定了⽤s1。
完整写法:    @PropertySource(value = {"classpath:xxx.properties"})
@ImportResource:
@ImportResource:导⼊Spring的配置⽂件,让配置⽂件⾥⾯的内容⽣效; 
因为 Spring Boot⾥⾯没有Spring的配置⽂件,我们⾃⼰编写的配置⽂件,也不能⾃动识别;想让Spring的配置⽂件⽣效,加载进来就在主配置类上⽤这个注解,相当于导⼊了Spring配置⽂件xml
主配置类其实就是那个⼊⼝函数。
测试:新建⼀个Spring配置⽂件 => 在主配置类 Application.java 使⽤@ImportResource注解 => ⽤SpringBoot单元测试测试配置⽂件是否存在Bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd">
<!--    创建Bean s1  -->
<bean id="s1" class="com.bihu.Bean.JavaBean"></bean>
</beans>
主配置类 Application.java:
package com.bihu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import t.annotation.ImportResource;
@ImportResource(locations = {"l"})  //导⼊Spring配置⽂件,可以导⼊多个【注意属性是locations】
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
主配置类
Test.java :测试类:
package com.bihu;
import com.bihu.Bean.JavaBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import t.ApplicationContext;
import st.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
//测试Bean
@Autowired
ApplicationContext ioc;
@Test
public void contextLoads() {
//如果主配置类未导⼊@ImportResource注解,那么会显⽰false
System.out.ainsBean("s1"));    //查看Spring配置⽂件是否存在Bean : s1
}
}
View Code
运⾏可以发现是teue,所以说,Spring配置可以导⼊多个,但如果不⽤@ImportSource 相当于没有,其次注意不是Value属性是  locations 属性!
我们都是SpringBoot 了,怎么可能还⽤那种?退化?我们要⽤全注解⽅式:
SpringBoot推荐给容器中添加组件的⽅式;推荐使⽤全注解的⽅式
配置⽂件 =  配置类所以我们⽤配置类!
1、@Configuration :指明⼀个类是配置类,⽤来替代Spring配置⽂件。
2、使⽤@Bean给容器中添加组件
其中:配置类中⽅法名相当于组件默认ID,返回的值会添加到容器中。
新建⼀个配置类:
package fig;
import com.bihu.Contorller.HelloController;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration  //指明当前类是配置类,⽤来替代Spring配置⽂件
public class MyAppConfig {
@Bean  //将⽅法的返回值添加到容器中;容器中这个组件默认的id就是⽅法名
public HelloController helloController(){
System.out.println("注解@Bean⽣效组件返回值将被加⼊到容器默认id为:helloController");
spring framework组件
return new HelloController();
}
}
WeAppConfig配置类
然后在测试⽂件测试看下  helloController  组件是否被加⼊到容器:
package com.bihu;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.context.SpringBootTest;
import t.ApplicationContext;
import st.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
ApplicationContext ioc;
@Test
public void contextLoads() {
System.out.ainsBean("helloController"));
}
}
SpringBoot测试⽂件Test
运⾏结果:
所以我们⽤配置类就好了他制动会执⾏加⼊的。。。其他的你也要懂。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。