@Properties:记载指定的配置文件

比如如果要加载person.properties
SpringBoot_@PropertiesSource,@ImportResource,@Bean_xml
SpringBoot_@PropertiesSource,@ImportResource,@Bean_单元测试_02
需要在实体类Person上加上@PropertiesSource注解
SpringBoot_@PropertiesSource,@ImportResource,@Bean_spring_03

SpringBoot_@PropertiesSource,@ImportResource,@Bean_spring_04

@ImportResource:给容器添加组件

如果是添加了一个beans.xml


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">



SpringBoot_@PropertiesSource,@ImportResource,@Bean_xml_05
SpringBoot_@PropertiesSource,@ImportResource,@Bean_spring_06
需要在运行类中加上
@ImportResource(locations = {“classpath:beans.xml”})

SpringBoot_@PropertiesSource,@ImportResource,@Bean_单元测试_07

利用SpringBoot单元测试一下

    @Autowired
ApplicationContext ioc;
@Test
public void test2(){
boolean helloService = ioc.containsBean("helloService");
System.out.println(helloService);
}

SpringBoot_@PropertiesSource,@ImportResource,@Bean_单元测试_08

结果为true.就是说beans,xml组件在容器里添加了

@Bean给容器添加配置类

SpringBoot_@PropertiesSource,@ImportResource,@Bean_xml_09
MyAppConfig.java

@Configuration//指明当前是一个配置类,替代之前的Spring配置文件
public class MyAppConfig {

@Bean
public HelloService helloService(){
System.out.println("1");
return new HelloService();
}

}

运用SpringBoot的单元测试

    @Autowired
ApplicationContext ioc;
@Test
public void test2(){
boolean helloService = ioc.containsBean("helloService");
System.out.println(helloService);
}

SpringBoot_@PropertiesSource,@ImportResource,@Bean_xml_10

添加到容器的组件中
指明当前是一个配置类,替代之前的Spring配置文件

最后加上Bean的注解也是SpringBoot推荐的一种添加到容器组件的方法