springboot @PropertySource

@ConfigurationProperties(prefix="person") 默认加载全局配置文件 application.properties或application.yml

application.properties文件中有字段 persion.first-name

@PropertySource 加载指定路径的配置文件信息

application.properties同级目录有person.properties first-name 如读取person.properties需要加@PropertySource  注解并指定路径@PropertySource(value = { "classpath:person.properties" })

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


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

    private String firstName;
    
    
    
}

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效 (@importResource标注在一个配置类上)

@ImportResource(location={"classpath:xxx.xml"})
导入spring的配置文件让其生效   

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

springboot推荐给容器中添加组件的方式:推荐使用全注解方式

1、配置类  相当于spring配置文件

2.使用@Bean 给容器添加组件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAppConfig {

    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean 
    public HelloService helloService(){
        return new HelloService();
    }
}
原文地址:https://www.cnblogs.com/slowcity/p/9098113.html