@PropertySource和@ImportResource

1.@PropertySource

@ConfigurationProperties是默认从全局配置文件中获取指定的值,比如

@ConfigurationProperties(prefix="person")这句话的意思是从application.yml或者application.properties中加载person的属性并映射到对应类的属性

那么如果我的person写在xxx.properties中,那么它就获取不到了

所以这个时候,我们便要加入注解@PropertySource(value={"classpath:xxx.properties"})//value里面的值是一个数组,可以多个值

2.@ImportResource:导入spring的配置文件,比如xxx.xml

比如我在bean.xml里面写了一个testService

<?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="testService" class="com.lyb.demo.service.testService">
    </bean>

</beans>

那么我在application主函数加入注解@ImportResource(locations={"classpath:bean.xml"})

就能把testService加入spring容器中

猜你喜欢

转载自blog.csdn.net/abc_123456___/article/details/91126042