SpringBoot配置(@PropertySource @ImportResource @Bean 注解)

 

(一):@PropertySource的意思是加载指定的配置文件

上一节中@ConfigurationProperties这个注解是去默认加载全局配置文件allication.properties中的属性。

但是,我们如果想加载其他配置文件中的属性呢?就需要在类中声明这个注解,并且指定他的具体位置和文件的名字.

我们创建一个名字叫做:person.properties的配置文件 。

(并把全局配置文件中的这些person的值都给注释掉,不然他回去全局文件中加载)


 
 
  1. #person.properties
  2. person.last-name=张三
  3. person.age= 21
  4. person.birth= 2017/ 12/ 15
  5. person.boss= false
  6. person.maps.k1=v1
  7. person.maps.k2= 14
  8. person.lists=a,b, c
  9. person.dog.name=小狗
  10. person.dog.age= 15

在类中加载这个文件


 
 
  1. package com.example.springboot02.bean;
  2. //加载指定的配置文件 并把值绑定到对应字段上
  3. @PropertySource(value = {"classpath:person.properties"})
  4. @Component
  5. @ConfigurationProperties(prefix = "person")
  6. public class Person2 {
  7. private String lastName;
  8. private Integer age;
  9. private Boolean boss;
  10. private Date birth;
  11. private Map<String,Object> maps;
  12. private List<Object> lists;
  13. private Dog dog;
  14. //自行补全get set toString方法
  15. }

我们去test中测试:


 
 
  1. package com.example.springboot02;
  2. @RunWith(SpringRunner.class)
  3. @SpringBootTest
  4. public class Springboot02ApplicationTests {
  5. @Autowired
  6. Person person;
  7. @Autowired
  8. Person2 person2;
  9. @Test
  10. public void contextLoads() {
  11. //System.out.println(person);
  12. System.out.println(person2);
  13. }
  14. }

结果就是我们配置文件中定义的值。测试成功

 

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

以前的做法:

我们自己定义一个类  假如叫做HelloService.java

然后定义一个xml文件  就叫beans.xml


 
 
  1. <?xml version="1.0" encoding="UTF‐8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema‐instance"
  4. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring‐beans.xsd">
  6.   <bean id="helloService" class="com.atguigu.springboot.service.HelloService"> </bean>
  7.   </beans>

我们去test中测试ioc中有没有这个容器


 
 
  1. package com.example.springboot02;
  2. import com.example.springboot02.bean.Person;
  3. import com.example.springboot02.bean.Person2;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. @ RunWith( SpringRunner. class)
  11. @ SpringBootTest
  12. public class Springboot02ApplicationTests {
  13. @ Autowired
  14. ApplicationContext ioc;
  15. @ Test
  16. public void testHelloService(){
  17. boolean b = ioc.containsBean( "helloService");
  18. System.out. println(b);
  19. }
  20. }

结果显示不存在。

那么怎么办呢?--->我们把@ImportResource标注在一个配置类上


 
 
  1. /*
  2. 入口类
  3. */
  4. @ImportResource(locations = { "classpath:beans.xml"})
  5. @SpringBootApplication
  6. public class Springboot02Application {
  7. public static void main(String[] args) {
  8. SpringApplication.run(Springboot02Application.class, args);
  9. }
  10. }

这是我们再测试一下,就说ioc容器中存在。

 

以上就是以前的做法,但是SpringBoot推荐的做法是什么呢?

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

SpringBoot推荐给容器中添加组件的方式;

推荐使用全注解的方式

1.创建一个config包。

2.创建一个配置类MyAppConfig.java

添加一个注解

 @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件

 
 

 


 
 
  1. package com.example.springboot02.config;
  2. import com.example.springboot02.service.HelloService;
  3. import org.springframework.context. annotation.Bean;
  4. import org.springframework.context. annotation.Configuration;
  5. @Configuration
  6. public class MyAppConfig {
  7. //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
  8. @Bean
  9. public HelloService helloService02(){
  10. System. out.println( "配置类@Bean给容器中添加组件了...");
  11. return new HelloService();
  12. }
  13. }

这时候应该是我们已经把

HelloService注册到ioc中了  而且他的id是helloService02
 
 

 

我们去测试:

test:

结果:

  存在!!!ok

 

 

下一节:第六章:SpringBoot配置(配置文件占位符)

 
 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值