How to access @ConfigurationProperties bean by it's name from XML configuration?












1















Suppose I have an AppProperties POJO annotated with @ConfigurationProperties("app"). It contains different app properties which I usually use to configure my beans in @Configuration classes.
@EnableConfigurationProperties(AppProperties.class) makes it available for @Autowiring, this is very convenient for Java-configuration.



But one part of my app's context is configured using old-fashined XML-confiuration. I would like to know how can I access @ConfigurationProperties AppProperties bean in XML configuration? If only @EnableConfigurationProperties provided me an ability to give a name, I could probably use SpEL in XML like this: #{appProperties.requiredProp} (I would really like to achieve it).



Unfortunately I do not see a way to provide the name and my attempt to use suggested appProperties name fails with:




SpelEvaluationException: EL1008E: Property or field 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?




I've seen in debugger that the bean is actually called app-my.package.AppProperties which is not obvious and never stated explicitly.



In JavaDoc for @EnableConfigurationProperties I noticed the following:




@ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this (@EnableConfigurationProperties) annotation.




Does this mean I can somehow get a named instance of AppProperties with values injected from application.properties using either @Bean method in Java or <bean/> in XML?



Of course, I could autowire the whole AppProperties to the class configured with XML, but I do not think it is a good design solution, because am only interested in a single String property from AppProperties. Probably I should not try to use this "type-safe" properties manner in XML and just stick to old way: using ${}-resolution, but I feel like I am missing something in the @ConfigurationProperties concept so please let me know.










share|improve this question





























    1















    Suppose I have an AppProperties POJO annotated with @ConfigurationProperties("app"). It contains different app properties which I usually use to configure my beans in @Configuration classes.
    @EnableConfigurationProperties(AppProperties.class) makes it available for @Autowiring, this is very convenient for Java-configuration.



    But one part of my app's context is configured using old-fashined XML-confiuration. I would like to know how can I access @ConfigurationProperties AppProperties bean in XML configuration? If only @EnableConfigurationProperties provided me an ability to give a name, I could probably use SpEL in XML like this: #{appProperties.requiredProp} (I would really like to achieve it).



    Unfortunately I do not see a way to provide the name and my attempt to use suggested appProperties name fails with:




    SpelEvaluationException: EL1008E: Property or field 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?




    I've seen in debugger that the bean is actually called app-my.package.AppProperties which is not obvious and never stated explicitly.



    In JavaDoc for @EnableConfigurationProperties I noticed the following:




    @ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this (@EnableConfigurationProperties) annotation.




    Does this mean I can somehow get a named instance of AppProperties with values injected from application.properties using either @Bean method in Java or <bean/> in XML?



    Of course, I could autowire the whole AppProperties to the class configured with XML, but I do not think it is a good design solution, because am only interested in a single String property from AppProperties. Probably I should not try to use this "type-safe" properties manner in XML and just stick to old way: using ${}-resolution, but I feel like I am missing something in the @ConfigurationProperties concept so please let me know.










    share|improve this question



























      1












      1








      1








      Suppose I have an AppProperties POJO annotated with @ConfigurationProperties("app"). It contains different app properties which I usually use to configure my beans in @Configuration classes.
      @EnableConfigurationProperties(AppProperties.class) makes it available for @Autowiring, this is very convenient for Java-configuration.



      But one part of my app's context is configured using old-fashined XML-confiuration. I would like to know how can I access @ConfigurationProperties AppProperties bean in XML configuration? If only @EnableConfigurationProperties provided me an ability to give a name, I could probably use SpEL in XML like this: #{appProperties.requiredProp} (I would really like to achieve it).



      Unfortunately I do not see a way to provide the name and my attempt to use suggested appProperties name fails with:




      SpelEvaluationException: EL1008E: Property or field 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?




      I've seen in debugger that the bean is actually called app-my.package.AppProperties which is not obvious and never stated explicitly.



      In JavaDoc for @EnableConfigurationProperties I noticed the following:




      @ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this (@EnableConfigurationProperties) annotation.




      Does this mean I can somehow get a named instance of AppProperties with values injected from application.properties using either @Bean method in Java or <bean/> in XML?



      Of course, I could autowire the whole AppProperties to the class configured with XML, but I do not think it is a good design solution, because am only interested in a single String property from AppProperties. Probably I should not try to use this "type-safe" properties manner in XML and just stick to old way: using ${}-resolution, but I feel like I am missing something in the @ConfigurationProperties concept so please let me know.










      share|improve this question
















      Suppose I have an AppProperties POJO annotated with @ConfigurationProperties("app"). It contains different app properties which I usually use to configure my beans in @Configuration classes.
      @EnableConfigurationProperties(AppProperties.class) makes it available for @Autowiring, this is very convenient for Java-configuration.



      But one part of my app's context is configured using old-fashined XML-confiuration. I would like to know how can I access @ConfigurationProperties AppProperties bean in XML configuration? If only @EnableConfigurationProperties provided me an ability to give a name, I could probably use SpEL in XML like this: #{appProperties.requiredProp} (I would really like to achieve it).



      Unfortunately I do not see a way to provide the name and my attempt to use suggested appProperties name fails with:




      SpelEvaluationException: EL1008E: Property or field 'appProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?




      I've seen in debugger that the bean is actually called app-my.package.AppProperties which is not obvious and never stated explicitly.



      In JavaDoc for @EnableConfigurationProperties I noticed the following:




      @ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this (@EnableConfigurationProperties) annotation.




      Does this mean I can somehow get a named instance of AppProperties with values injected from application.properties using either @Bean method in Java or <bean/> in XML?



      Of course, I could autowire the whole AppProperties to the class configured with XML, but I do not think it is a good design solution, because am only interested in a single String property from AppProperties. Probably I should not try to use this "type-safe" properties manner in XML and just stick to old way: using ${}-resolution, but I feel like I am missing something in the @ConfigurationProperties concept so please let me know.







      spring spring-boot






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 22:33







      Derp

















      asked Jan 1 at 22:20









      DerpDerp

      1,22011428




      1,22011428
























          1 Answer
          1






          active

          oldest

          votes


















          0














          There are two ways to register a configuration properties bean.



          Registering with @EnableConfigurationProperties.



           @EnableConfigurationProperties(AppProperties.class)
          public class Application {}

          @ConfigurationProperties("app")
          public class AppProperties {}


          @EnableConfigurationProperties annotation will automatically register AppProperties class as a bean called prefix-your.package.AppProperties, when you include the class as follows: @EnableConfigurationProperties(AppProperties.class) and AppProperties has got a prefix, @ConfigurationProperties("app").



          Registering with @Component or @Bean



           @EnableConfigurationProperties
          public class Application {}

          @Component
          @ConfigurationProperties("app")
          public class AppProperties {}


          or



          @EnableConfigurationProperties
          public class Application {

          @Bean
          @ConfigurationProperties("app")
          public AppProperties appProperties() {
          return new AppProperties();
          }

          }


          The @Component and @Bean will register a bean called appProperties.
          By using the second approach you would be able to use SpEL to get the property #{appProperties.requiredProp}






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999391%2fhow-to-access-configurationproperties-bean-by-its-name-from-xml-configuration%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            There are two ways to register a configuration properties bean.



            Registering with @EnableConfigurationProperties.



             @EnableConfigurationProperties(AppProperties.class)
            public class Application {}

            @ConfigurationProperties("app")
            public class AppProperties {}


            @EnableConfigurationProperties annotation will automatically register AppProperties class as a bean called prefix-your.package.AppProperties, when you include the class as follows: @EnableConfigurationProperties(AppProperties.class) and AppProperties has got a prefix, @ConfigurationProperties("app").



            Registering with @Component or @Bean



             @EnableConfigurationProperties
            public class Application {}

            @Component
            @ConfigurationProperties("app")
            public class AppProperties {}


            or



            @EnableConfigurationProperties
            public class Application {

            @Bean
            @ConfigurationProperties("app")
            public AppProperties appProperties() {
            return new AppProperties();
            }

            }


            The @Component and @Bean will register a bean called appProperties.
            By using the second approach you would be able to use SpEL to get the property #{appProperties.requiredProp}






            share|improve this answer






























              0














              There are two ways to register a configuration properties bean.



              Registering with @EnableConfigurationProperties.



               @EnableConfigurationProperties(AppProperties.class)
              public class Application {}

              @ConfigurationProperties("app")
              public class AppProperties {}


              @EnableConfigurationProperties annotation will automatically register AppProperties class as a bean called prefix-your.package.AppProperties, when you include the class as follows: @EnableConfigurationProperties(AppProperties.class) and AppProperties has got a prefix, @ConfigurationProperties("app").



              Registering with @Component or @Bean



               @EnableConfigurationProperties
              public class Application {}

              @Component
              @ConfigurationProperties("app")
              public class AppProperties {}


              or



              @EnableConfigurationProperties
              public class Application {

              @Bean
              @ConfigurationProperties("app")
              public AppProperties appProperties() {
              return new AppProperties();
              }

              }


              The @Component and @Bean will register a bean called appProperties.
              By using the second approach you would be able to use SpEL to get the property #{appProperties.requiredProp}






              share|improve this answer




























                0












                0








                0







                There are two ways to register a configuration properties bean.



                Registering with @EnableConfigurationProperties.



                 @EnableConfigurationProperties(AppProperties.class)
                public class Application {}

                @ConfigurationProperties("app")
                public class AppProperties {}


                @EnableConfigurationProperties annotation will automatically register AppProperties class as a bean called prefix-your.package.AppProperties, when you include the class as follows: @EnableConfigurationProperties(AppProperties.class) and AppProperties has got a prefix, @ConfigurationProperties("app").



                Registering with @Component or @Bean



                 @EnableConfigurationProperties
                public class Application {}

                @Component
                @ConfigurationProperties("app")
                public class AppProperties {}


                or



                @EnableConfigurationProperties
                public class Application {

                @Bean
                @ConfigurationProperties("app")
                public AppProperties appProperties() {
                return new AppProperties();
                }

                }


                The @Component and @Bean will register a bean called appProperties.
                By using the second approach you would be able to use SpEL to get the property #{appProperties.requiredProp}






                share|improve this answer















                There are two ways to register a configuration properties bean.



                Registering with @EnableConfigurationProperties.



                 @EnableConfigurationProperties(AppProperties.class)
                public class Application {}

                @ConfigurationProperties("app")
                public class AppProperties {}


                @EnableConfigurationProperties annotation will automatically register AppProperties class as a bean called prefix-your.package.AppProperties, when you include the class as follows: @EnableConfigurationProperties(AppProperties.class) and AppProperties has got a prefix, @ConfigurationProperties("app").



                Registering with @Component or @Bean



                 @EnableConfigurationProperties
                public class Application {}

                @Component
                @ConfigurationProperties("app")
                public class AppProperties {}


                or



                @EnableConfigurationProperties
                public class Application {

                @Bean
                @ConfigurationProperties("app")
                public AppProperties appProperties() {
                return new AppProperties();
                }

                }


                The @Component and @Bean will register a bean called appProperties.
                By using the second approach you would be able to use SpEL to get the property #{appProperties.requiredProp}







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 2 at 9:16

























                answered Jan 2 at 9:11









                Rentius2407Rentius2407

                5421523




                5421523
































                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999391%2fhow-to-access-configurationproperties-bean-by-its-name-from-xml-configuration%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory