@propertysource
加载指定的配置文件。
@PropertySource(value={"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated//校验
public class person {
//@Value("${person.lastname}")//赋值
//@Email//lastname 必须填成邮箱格式和@validated配套使用
private String lastname;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object>maps;
private List<Object> lists;
private Dog dog;
@importresource导入spring中的配置文件,让配置文件的内容生效。
我们自己编写的beans.xml没用,spring boot里面没有spring的配置文件,
想让spring配置加载进来需要这个注解。
标注在一个配置类上。
在主函数中
@ImportResource(locations = {"classpath:beans.xml"})
不编写spring的配置文件了。
<?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.atguigu.springboot.service.helloservice"></bean>
</beans>
spring boot推荐的给容器中添加组件的方式:
1.配置类==配置文件
@Configuration//指明当前类是一个配置类
//就是来替代spring配置文件
public class myappconfig {
@Bean//这个注解来添加容器
//将方法的返回值添加到容器中 容器中这个组件默认的id是这个方法名
public helloservice helloservice1()//测试时返回的是函数名是后面的这个helloservice
{
System.out.println("添加组件了");
return new helloservice();
}
测试模块的代码
@Test
public void testhelloservice()
{
boolean b=ioc.containsBean("helloservice1");//双引号里要和上面的函数名相同。
System.out.println(b);
}