Sometimes there are situations like this, we want information about the configuration file, read and automatically packaged into an entity class, like this, we use much easier and more convenient in the code which, this time, we can use @ConfigurationProperties, it the same configuration information may be automatically packaged into an entity class
First, the configuration file which this information is like drops
connection.username=admin connection.password=kyjufskifas2jsfs connection.remoteAddress=192.168.1.1
At this time we can define an entity class information in the configuration file is loaded
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { private String username; private String remoteAddress; private String password ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getRemoteAddress() { return remoteAddress; } public void setRemoteAddress(String remoteAddress) { this.remoteAddress = remoteAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
We can also put @ConfigurationProperties also be defined directly on @bean notes, which is the entity bean class do not have the @Component and @ConfigurationProperties
@SpringBootApplication public class DemoApplication{ //... @Bean @ConfigurationProperties(prefix = "connection") public ConnectionSettings connectionSettings(){ return new ConnectionSettings(); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Then we need to use this direct injection
@RestController @RequestMapping("/task") public class TaskController { @Autowired ConnectionSettings conn; @RequestMapping(value = {"/",""}) public String hellTask(){ String userName = conn.getUsername(); return "hello task !!"; } }
If you find @ConfigurationPropertie not take effect, there may be structural problems in the project directory,
You may need to explicitly specify which entity classes by using @EnableConfigurationProperties (ConnectionSettings.class) to load the configuration information
@Configuration @EnableConfigurationProperties(ConnectionSettings.class) public class MailConfiguration { @Autowired private MailProperties mailProperties; @Bean public JavaMailSender javaMailSender() { // omitted for readability } }
Sometimes there are situations like this, we want information about the configuration file, read and automatically packaged into an entity class, like this, we use much easier and more convenient in the code which, this time, we can use @ConfigurationProperties, it the same configuration information may be automatically packaged into an entity class
First, the configuration file which this information is like drops
connection.username=admin connection.password=kyjufskifas2jsfs connection.remoteAddress=192.168.1.1
At this time we can define an entity class information in the configuration file is loaded
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { private String username; private String remoteAddress; private String password ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getRemoteAddress() { return remoteAddress; } public void setRemoteAddress(String remoteAddress) { this.remoteAddress = remoteAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
We can also put @ConfigurationProperties also be defined directly on @bean notes, which is the entity bean class do not have the @Component and @ConfigurationProperties
@SpringBootApplication public class DemoApplication{ //... @Bean @ConfigurationProperties(prefix = "connection") public ConnectionSettings connectionSettings(){ return new ConnectionSettings(); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Then we need to use this direct injection
@RestController @RequestMapping("/task") public class TaskController { @Autowired ConnectionSettings conn; @RequestMapping(value = {"/",""}) public String hellTask(){ String userName = conn.getUsername(); return "hello task !!"; } }
If you find @ConfigurationPropertie not take effect, there may be structural problems in the project directory,
You may need to explicitly specify which entity classes by using @EnableConfigurationProperties (ConnectionSettings.class) to load the configuration information
@Configuration @EnableConfigurationProperties(ConnectionSettings.class) public class MailConfiguration { @Autowired private MailProperties mailProperties; @Bean public JavaMailSender javaMailSender() { // omitted for readability } }