注解@PropertySource 可以指定从哪个配置文件中获取值,用法如下:
@PropertySource(value = { "classpath:配置文件的名字" })
@importResource :导入Spring的配置文件,让配置文件里面的内容生效;
之前使用spring时,比如我们创建一个helloService的组件,首先创建一个HelloService类,然后创建xml文件, 需要写<bean class="" id="helloService"></bean>
然后在spring boot 中生效 需要将这个配置导入到主类,就需要这个注解了
@PropertySource(value = { "classpath:HelloService" })
实际上在spring boot 开发中 都会使用其 推荐的方式:推荐使用全注解方式
1、配置类 === spring配置文件
2、使用@Bean给容器中添加组件
/**
*@Configuration:指明当前类是一个配置类,就是来代替之前的spring配置文件
*在配置文件中使用<bean>标签添加组件
*/
@Configuration
public class MyAppConfig {
//@Bean 将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
@Bean(name = "template")
public HelloService helloService(){
return new HelloService();
}
}