跟我学springboot(九)springboot注入@PropertySource&@ImportResource&@Bean

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a303549861/article/details/82393444

1.@PropertySource:加载指定的配置文件

新建配置文件person.properties

person.last-name=李四
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

在类上添加注解:

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person

除此之外还有其他属性,比如:
@PropertySource(value= {“classpath:config/jdbc-bainuo-dev.properties”},ignoreResourceNotFound=false,encoding=”UTF-8”,name=”jdbc-bainuo-dev.properties”,)
上述的代码目的是加载classpath路径中config文件中的jdbc-bainuo-dev.properties。其中encoding

用于指定读取属性文件所使用的编码,我们通常使用的是UTF-8;ignoreResourceNotFound含义是当指定的配置文件不存在是否报错,默认是false;比如上文中指定的加载属性文件是jdbc-bainuo-dev.properties。如果该文件不存在,则ignoreResourceNotFound为true的时候,程序不会报错,如果ignoreResourceNotFound为false的时候,程序直接报错。实际项目开发中,最好设置ignoreResourceNotFound为false。该参数默认值为false。

value值是设置需要加载的属性文件,可以一次性加载多个。name的值我们设置的是jdbc-bainuo-dev.properties。这个值在Springboot的环境中必须是唯一的,如果不设置,则值为:“class path resource [config/jdbc-bainuo-dev.properties]“。

可能很多人比较纳闷,为什么是“class path resource [config/jdbc-bainuo-dev.properties]“呢?这个就涉及到了Spring中对资源文件的封装类Resource。上文我们配置的value值为”classpath:config/jdbc-bainuo-dev.properties”,因此Spring发现是classpath开头的,因此最终使用的是Resource的子类ClassPathResource。如果是file开头的,则最终使用的类是FileSystemResource。

了解了上文所述的Resource类之后。我们再次明确一点,如果@PropertySource中如果没有设置name值,则name值的生成规则是:根据value值查找到最终封装的Resource子类,然后调用具体的Resource子类实例对象中的getDescription方法,getDescription方法的返回值为最终的name值。

2.@ImportResource

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

编写bean.xml

<?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>
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {

3.SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式

1、配置类@Configuration——>Spring配置文件
2、使用@Bean给容器中添加组件

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

    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService02(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }

}

测试:

@Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(){
        boolean b = ioc.containsBean("helloService02");
        System.out.println(b);
    }

猜你喜欢

转载自blog.csdn.net/a303549861/article/details/82393444
今日推荐