添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
强悍的梨子  ·  python ...·  5 月前    · 

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.
                    This is interesting - I do not see the point of the bean method now. Couldn't one directly @Autowired the DataSource class ?
                        – tMJ
                    Apr 6 '17 at 11:37
                    @tMJ Imagine you have multiple database configurations. You could extend the DataSource class multiple times for each seperate database and add the @ConfigurationProperties to each one, Or you could create a bean for each one with the appropriate property prefix. e.g. (@ConfigurationProperties(prefix="spring.datasource.test") @Bean public DataSource testDataSource(){...}). This saves the redundancy of creating each class for the sake of seperate property sets.
                        – coderatchet
                    Sep 21 '17 at 6:58
                    @EvgeniDimitrov Thanks, it actually only occurred to me because I was looking for this very solution to my problem and this fit my current use case as described perfectly.
                        – coderatchet
                    Sep 21 '17 at 23:39
                    Just to point out that setters are important in the destination class (eg. Datasource). I thought it would use reflection for the mapping but it uses the setters instead.
                        – nik686
                    Jan 9 '18 at 23:05
            

    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