1. Spring Boot default configuration file application.properties or appliaction.yml
2. There are several ways to customize attributes:
2.1 For reference to a few attribute values, you can directly reference the custom property value directly using the @value annotation, without needing annotation mates such as @configuration.
Application.properties provides support for custom attributes so that we can configure some constants here:
Com.windy.name= "Windy" com.windy.want= "I wish you all the best of the New Year"
You can then bind to the property you want directly by using the annotation @value (value= "${config.name}") where you want to use it.
@RestControllerpublic class Usercontroller { @Value ("${com.windy.name}") private String name; @Value ("${com.windy.want}") private String want; @RequestMapping ("/") Public String Hexo () { return name+ "," +want; }}
2.2 Sometimes too many properties, binding too tired, the official advocate to bind an object bean.
Implementing this approach requires 3 steps:
Here we create a new Java class Configbean, and add annotation @configuration to this class (this note is missing)
@Configuration @configurationproperties (prefix = "Com.windy")//@PropertySource (classpath:application.properties) public class Configbean { private String name; Private String want; Omit getter and Setter}
You also need to add @enableconfigurationproperties to the Spring boot entry class
@SpringBootApplication @enableconfigurationproperties () public class Chapter2application {public static void main (string[] args) { Springapplication.run (chapter2application.class, args);} }
Finally, the controller is introduced into the Configbean to use
@RestControllerpublic class Usercontroller { @Autowired configbean Configbean; @RequestMapping ("/") Public String Hexo () { return configbean.getname () +configbean.getwant ();} }
3. Priority of the configuration file
Application.properties and Application.yml files can be placed in the following four locations:
- External, in the/congfig subdirectory relative to the application running directory.
- External, in the directory where the application is running
- Built-in, within config package
- Built-in, classpath root directory
Again, this list is sorted by priority, that is, Src/main/resources/config under Application.properties overrides src/main/ Resources under the same properties in Application.properties,
In addition, if you have both Application.properties and application.yml in the same priority position, the attributes inside the application.properties will overwrite the application.yml.
4. Configuring with @profile annotations
First, define a Dbconnect interface
@Componentpublic interface Dbconnector {public void Configure ();}
Then define the implementation classes for MySQL and Oracle separately.
@Component @profile ("Mysql") public class Mysqlconnector implements Dbconnector {@Overridepublic void Configure () { System.out.println ("This is mysql!");}}
@Component @profile ("Oracle") public class Oracleconnector implements Dbconnector {@Overridepublic void Configure () { System.out.println ("This is oracle!");}}
Specify which implementation class to use in configuration file application.properties
Spring.profiles.active=oracle
And then you can use it.
@RestControllerpublic class Dbcontorller {@AutowiredDBConnector dbconnector; @RequestMapping ("/db") public void Dbconnector () {dbconnector.configure ();//FINAL print "This Is Oracle!"}}
In addition to spring.profiles.active to activate one or more profiles, you can use Spring.profiles.include to overlay the profile
Spring.profiles.active:Oracle SPRING.PROFILES.INCLUDE:PRODDB,PRODMQ
Spring Boot Property Configuration