8 读取指定配置文件

1 PropertySource

1.1 加入注解,指定配置文件
package com.gp6.springboot08.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:property-source.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;

    @Override
    public String toString() {
        return "Person{" + "lastName='" + lastName + '\'' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}';
    }
	get...
	set...
}
1.2 person-source.properties
  • 要将全局配置文件中的person注释,否则优先读取全局配置文件
person.last-name=gp6
person.age=32
person.birth=2018/11/19
person.boss=true
person.maps.k1=神舟
person.maps.k2=联想
person.lists=火箭,雷霆,勇士,馬刺
person.dog.name=dog
person.dog.age=15
1.3 测试
package com.gp6.springboot08;

import com.gp6.springboot08.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.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestPropertySource {
    @Autowired
    Person person;

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

在这里插入图片描述

2 ImportResource

  • 导入Spring的配置文件,让配置文件里面的内容生效
2.1 配置文件
<?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="userServiceBean" class="com.gp6.springboot08.service.UserService"/>
</beans>
2.2 测试容器中是否存在hello
package com.gp6.springboot08;

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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestImportSource {
    @Autowired
    ApplicationContext ioc;

    @Test
    public void testUserService() {
        System.out.println(ioc.containsBean("userServiceBean"));
    }
}

测试结果

2.3 结论
SpringBoot里没有Spring的配置文件,自定义的配置文件,不能自动识别;
2.4 是Spring配置文件生效
@ImportResource标注在一个配置类上,即可解决
@ImportResource(locations = {"classpath:bean.xml"})
public class HelloApplication {

运行结果

##3 SpringBoot使用全注解

1: 配置类@Configuration———>Spring配置文件
2: 使用@Bean给容器中添加组件
3.1 配置类
package com.gp6.springboot08.config;

import com.gp6.springboot08.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

         @Bean等效

         将方法的返回值添加到容器中;
     */
    @Bean
    public UserService userServiceConfig(){
        System.out.println("配置类@Bean向容器中添加组件...");
        return new UserService();
    }
}

3.2 测试
  • 将ImportResource注释
//@ImportResource(locations = {"classpath:bean.xml"})
public class HelloApplication {
  • 测试
	@Test
    public void testUserService2() {
        System.out.println(ioc.containsBean("userServiceConfig"));
    }

测试结果

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值