1. @PropertySource
作用:加载指定的配置文件
@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = {"classpath:people.properties"})
public class People {
2. @ImportResource
作用:导入spring的配置文件,,让配置文件里面的内容生效,
SpringBoot里面没有Spring的配置文件,我们自己编写的配置文件,不能自动识别.
想让自己编写的spring配置文件生效,将@ImportResource标注在主配置类上.
@SpringBootApplication
@ImportResource(value = {"classpath:beans.xml"})
public class Springboot01HelloworldQuickApplication {
3. SpringBoot推荐给容器添加组建的方式
不推荐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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloService" class="com.jfh.springboot01helloworldquick.service.HelloService"/>
</beans>
springboot 推荐给容器中添加组件的方式:全注解方式
- 配置类====spring配置文件
- 使用@Bean往容器中添加组件
/**该注解标明当前类是一个配置类,替代之前的spring配置文件
* 在配置文件中<bean></bean>标签添加组件
*/
@Configuration
public class MyConfig {
/**
* 将方法的返回值添加到容器中,容器中组件默认id为该方法名
* @return
*/
@Bean
public HelloService helloService(){
return new HelloService();
}
}