@ConfigurationProperties (prefix= "person") load global profile application.properties or APPLICATION.YML by default
There are fields in the Application.properties file Persion.first-name
@PropertySource load profile information for the specified path
Application.properties Sibling directory has person.properties first-name such as read person.properties need to add @propertysource Note and specify the path @propertysource (value = {"Classpath:person.properties"})
Import org.springframework.boot.context.properties.ConfigurationProperties; Import Org.springframework.context.annotation.PropertySource; Import = {"Classpath:person.properties" }) @ConfigurationProperties (prefix= "person") publicclass person { private String firstName; }
@ImportResource: Import the Spring configuration file and let the contents of the configuration file take effect (@importResource label on a configuration Class)
@ImportResource (location={"Classpath:xxx.xml"}) Import the spring configuration file for it to take effect
@Configuration: Indicates that the current class is a configuration class, that is, to replace the previous spring configuration file, and to add components with <bean><bean/> tags in the configuration file
Springboot recommended ways to add components to a container: full annotation recommended
1. Configuration class equivalent to spring configuration file
2. Adding components to a container using @bean
Import Org.springframework.context.annotation.Bean; Import org.springframework.context.annotation.Configuration; @Configuration Public class myappconfig { // adds the return value of the method to the container; The default ID of this component in the container is the method name @Bean public HelloService HelloService () { returnnew HelloService ();} }
Springboot @PropertySource