Could someone give a MWE of how to use the
@ConfigurationProperties
annotation directly on a
@Bean
method?
I have seen countless examples of it being used on class definitions - but no examples yet for
@Bean
methods.
To quote the
documentation
:
Add this to a class definition or
a
@Bean
method
@Target(value={TYPE,
METHOD
})
So, I think there is a possibility and an intended use as well - but unluckily I am unable to figure it out.
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return new DataSource();
Here the DataSource class has proeprties url, username, password, driverClassName, so spring boot maps them to the created object.
Example of the DataSource class:
public class DataSource {
private String url;
private String driverClassName;
private String username;
private String password;
//getters & setters, etc.
In other words this has the same effect as if you initialize some bean with stereotype annotations(@Component, @Service, etc.)
@Component
@ConfigurationProperties(prefix="spring.datasource")
public class DataSource {
private String url;
private String driverClassName;
private String username;
private String password;
//getters & setters, etc.
–
–
–
–
24.8.1 Third-party Configuration
As well as using @ConfigurationProperties
to annotate a class, you can also use it on public @Bean
methods. Doing so can be particularly useful when you want to bind properties to third-party components that are outside of your control.
To configure a bean from the Environment properties, add @ConfigurationProperties
to its bean registration, as shown in the following example:
@ConfigurationProperties(prefix = "another")
@Bean
public AnotherComponent anotherComponent() {
Any property defined with the another prefix is mapped onto that AnotherComponent bean in manner similar to the preceding AcmeProperties example.
@ConfigurationProperties(prefix = "my.entity")
public MY_ENTITY getContract() {
return new MY_ENTITY()
.setProp1("prop1111111")
.setProp2("prop2222222")
@Bean(name = "contract2")
@ConfigurationProperties(prefix = "my.entity2")
public MY_ENTITY getContract2() {
return new MY_ENTITY()
.setProp1("prop1111.2222")
.setProp2("prop2222.222")
application.properties
my.entity.prop1=2120180023
my.entity.prop2=CUSTOMER_NAME111
my.entity2.prop1=9994494949
my.entity2.prop2=CUSTOMER_NAME222
SpringBootApplication
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
@Qualifier("contract2")
private MY_ENTITY myEntity;
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);
@Override
public void run(String... args) throws Exception {
System.out.println(myEntity);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0
with attribution required.
rev 2020.2.25.36142