Today's article introduces you to two ways of customizing configuration First: Use @Configuration Properties and look at the code
package com.developlee.customconfig.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.context.annotation.Configuration; /** * @author Lensen * @desc * @since 2018/8/22 12:59 */ @Configuration @ConfigurationProperties(prefix = "one-app") public class OneAppConfig { @NestedConfigurationProperty public Account account = new Account(); public String appName; public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public class Account { private String username; private String password; private String age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } }
Obviously, this is the configuration item we want to configure in the properties file. Look at the second way
/** * @author Lensen * @desc * @since 2018/8/22 13:19 */ @Configuration public class TwoAppConfig { @Value("${two-app.welcome.message}") public String twoAppWelcomeMessage; @Value("${two-app.welcome.person}") public String twoAppWelcomePerson; public String getTwoAppWelcomeMessage() { return twoAppWelcomeMessage; } public void setTwoAppWelcomeMessage(String twoAppWelcomeMessage) { this.twoAppWelcomeMessage = twoAppWelcomeMessage; } public String getTwoAppWelcomePerson() { return twoAppWelcomePerson; } public void setTwoAppWelcomePerson(String twoAppWelcomePerson) { this.twoAppWelcomePerson = twoAppWelcomePerson; } }
This is simple and rude. The structure of the first way is not so clear. How to use it depends entirely on the relationship and complexity of the project configuration items, which needs to be weighed according to the actual situation. Next, I wrote a simple test class to get our configuration information. Look at the configuration file first:
one-app: app-name: OneAPP account: username: Lensen password: Orcl age: 22 two-app: welcome: message: welcome to lensen's bolg person: LENSEN
A simple Controller class
package com.developlee.customconfig.controller; import com.developlee.customconfig.config.OneAppConfig; import com.developlee.customconfig.config.TwoAppConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Lensen * @desc * @since 2018/8/22 16:40 */ @RestController public class AppController { @Autowired private OneAppConfig oneAppConfig; @Autowired private TwoAppConfig twoAppConfig; @GetMapping("/hello") public ResponseEntity getConfig() { String str1 = "oneAppConfig: " + oneAppConfig.getAppName() + oneAppConfig.getAccount().getUsername() + oneAppConfig.getAccount().getPassword() + oneAppConfig.getAccount().getAge(); String str2 = "twoAppConfig: " + twoAppConfig.getTwoAppWelcomePerson() + twoAppConfig.getTwoAppWelcomeMessage(); return new ResponseEntity(str1 +"~~~~~~~"+ str2, HttpStatus.OK); } }
Enter http:localhost:8080/hello in the address bar and return
You can also specify files by yourself, just add the annotation @PropertySource annotation to the class ~~
@Configuration @PropertySource("classpath:my.properties") public class ThreeConfig { @Value("${my.name}") private String myName; public String getMyName() { return myName; } public void setMyName(String myName) { this.myName = myName; } }
my.properties file content:
my.name=developlee
Test results:
If the configuration file is in yml format, use the Yaml Properties FactoryBean to load and set it to the Property Sources Placeholder Configurer
// Loading YML Format Custom Profile @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new FileSystemResource("config.yml"));//Introduction of File // Yaml.setResources (new ClassPathResource ("youryml.yml"); introduction of //class configurer.setProperties(yaml.getObject()); return configurer;
end... Impetuous society, impetuous life, only code, quiet and far-reaching. (I'm sorry to start loading 13 again.)
Finally, the above sample code is available in my github.com Find it in. My personal public number: the smart life of developlee. Focus on not necessarily update, update is not necessary.