前几篇我们讲解了@ConfigurationProperties和@Value,我们再来说几个
@PropertySource
@PropertySource:@ConfigurationProperties默认是从全局配置文件中获取值,而@PropertySource则可以从指定的配置文件中获取值
我们来实验一下
先在resources下建立一个person.properties文件,然后把全局配置文件下的有关person的代码复制过去,把全局配置文件中的这段代码注释掉
person.last-name=张三
person.age=20
person.boss=false
person.birth=2019/12/12
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=white
person.dog.age=10
然后我们加上@PropertySource
/**
* 将配置文件中配置的没一个属性的值映射到这个组建中
* @configurationproperties:告诉springboot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix="person"配置文件中person下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties的功能
*/
@PropertySource(value = {"classpath:person.Properties"})
@Component
@ConfigurationProperties(prefix="person")
//@Validated
public class Person {
/*
<bean class="person">
<property name="lastName" value="自变量/$/{key}从环境变量配置文件中获取值/#{SpEL}"></property>
<bean/>
*/
//last-name必须为邮箱格式
//@Email
//@Value("${person.last-name}")
private String lastName;
//@Value("#{10*2}")
private Integer age;
// @Value("true")
private Boolean boss;
private Date birth;
//@Value("{person.maps}")
private Map<String,Object> maps;
private List<Object>lists;
private Dog dog;
测试一下,打开test,进行测试
我们发现值全部传过来了
@ImportResource
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效
我们来实验一下
首先在resources下建立一个beans.xml文件,(new,XML Configuration File,Spring Config)
然后我们再建立一个service包,下建立一个Helloservice文件
把这个文件加到spring的配置文件中,并且加一个id
<?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="com.example.demo.service.Helloservice"></bean>
</beans>
我们来实验一下我们自己编写的spring配置文件能不能使用
在test程序中加入测试程序
import java.applet.AppletContext;
/*
springboot单元测试
可以在测试期间类似于编码一样自动注入容器的功能
*/
@SpringBootTest
class DemoApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void TestHelloservice(){
boolean b=ioc.containsBean("Helloservice");
System.out.println(b);
}
@Test
void contextLoads() {
System.out.println(person);
}
}
测试结果,如果成功会打印出true,否则为false
测试结果为false
这说明了SpringBoot里面没有Spring的配置文件,自己编写的配置文件,也不能自动识别
想让spring的配置文件生效,加载进来,需要把@ImportResource标注在同一个配置类上
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在主程序上加@ImportResource注释
然后测试一下
这时我们发现结果为true,说明容器中已经有了配置文件
总结下@ImportResource作用:
导入spring的配置文件并让其生效
我们一个一个的给容器添加组件很麻烦,这里有一个SpringBoot推荐给容器中添加组件的方式:推荐使用全注解的方式
**1.配置类=========spring的配置文件**
我们来建立一个配置类(建立配置类之前把主程序中的@ImportResource注释掉),然后把里面的方法写好
/*
@Configuration说明当前类一个配置类,代替之前的spring的配置文件
在配置文件中用<bean><bean/>标签来添加组件
*/
@Configuration
public class MyAppConfig {
// @Bean将方法的返回值添加到容器中:容器中这个组件默认的id就是方法名
@Bean
public Helloservice helloservice(){
System.out.println("配置类@Bean为容器添加组件了...");
return new Helloservice();
}
}
测试一下
我们发现组件已经被添加到了容器中
2.使用@Bean给容器添加组件