@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(); } }