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"));
}