First, @PropertySource
@PropertySource: Load the specified configuration file
@PropertySource (value = {"Classpath:person.properties"= "Person")the publicclass Person {}
Second, @ImportResource: Import the Spring configuration file, let the contents of the configuration file take effect
Spring boot does not have a spring configuration file, and the configuration files we write ourselves are not automatically recognized;
Want spring's configuration file to take effect, load it in, @ImportResource label on a configuration class
@ImportResource (locations = {"Classpath:applicationContext.xml"}) @SpringBootApplication Public class springbootconfigapplication { publicstaticvoid main (string[] args) { Springapplication.run (springbootconfigapplication. class , args);} }
Third, Springboot recommend the way to add components to the container, the recommended way to use full annotation
1. Configuration class @configuration------>spring configuration file
2. Add components to the container using @bean
/** * @Configuration: Indicates that the current class is a configuration class; Replace the previous spring configuration file * * Add components with <bean><bean/> tags in the configuration file * */ @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 () {System.out.println ("configuration class @bean adding components to the container ..."); returnnew HelloService (); }}
Springboot configuring @PropertySource, @ImportResource, @Bean