Accessing spring application name in custom starter










1















Im trying to access the spring application name in a custom starter auto-configuration.



@Configuration
public class CustomAutoConfiguration

@Value("$spring.application.name")
private String appName;




spring.factories as,



org.springframework.boot.autoconfigure.EnableAutoConfiguration=
co.test.CustomAutoConfiguration


In an application that uses this custom starter I have defined the application name in bootstrap.yaml



spring:
application:
name: test-app


However, I'm seeing that appName is null. My guess is this has something to do with the order of loading ? Anyway to achieve this ?










share|improve this question




























    1















    Im trying to access the spring application name in a custom starter auto-configuration.



    @Configuration
    public class CustomAutoConfiguration

    @Value("$spring.application.name")
    private String appName;




    spring.factories as,



    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    co.test.CustomAutoConfiguration


    In an application that uses this custom starter I have defined the application name in bootstrap.yaml



    spring:
    application:
    name: test-app


    However, I'm seeing that appName is null. My guess is this has something to do with the order of loading ? Anyway to achieve this ?










    share|improve this question


























      1












      1








      1








      Im trying to access the spring application name in a custom starter auto-configuration.



      @Configuration
      public class CustomAutoConfiguration

      @Value("$spring.application.name")
      private String appName;




      spring.factories as,



      org.springframework.boot.autoconfigure.EnableAutoConfiguration=
      co.test.CustomAutoConfiguration


      In an application that uses this custom starter I have defined the application name in bootstrap.yaml



      spring:
      application:
      name: test-app


      However, I'm seeing that appName is null. My guess is this has something to do with the order of loading ? Anyway to achieve this ?










      share|improve this question
















      Im trying to access the spring application name in a custom starter auto-configuration.



      @Configuration
      public class CustomAutoConfiguration

      @Value("$spring.application.name")
      private String appName;




      spring.factories as,



      org.springframework.boot.autoconfigure.EnableAutoConfiguration=
      co.test.CustomAutoConfiguration


      In an application that uses this custom starter I have defined the application name in bootstrap.yaml



      spring:
      application:
      name: test-app


      However, I'm seeing that appName is null. My guess is this has something to do with the order of loading ? Anyway to achieve this ?







      java spring spring-boot






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 10:57









      Vadim Kotov

      4,53563347




      4,53563347










      asked Nov 13 '18 at 10:54









      nixgadgetsnixgadgets

      2,35484271




      2,35484271






















          4 Answers
          4






          active

          oldest

          votes


















          1














          I had similar issue in past and I solved by autowiring org.springframework.core.env.Environment; something like this:



          @Configuration
          public class CustomAutoConfiguration
          @Autowired
          private Evinronment env;
          private String appName;
          @PostConstruct
          public void initialize()
          this.appName = env.getProperty("spring.application.name");





          Not tested but it should work



          Angelo






          share|improve this answer























          • PostConstruct never gets called in my case.

            – nixgadgets
            Nov 13 '18 at 12:59


















          1














          This is what worked in the end.



          @Configuration
          public class CustomAutoConfiguration implements EnvironmentAware

          @Override
          public void setEnvironment(Environment environment)
          this.environment = environment;


          // And then accessing via this.environment.getProperty("spring.application.name")







          share|improve this answer






























            0














            Might have something to do with the order in which bootstrap.yml gets loaded. We uaw @Value all the time in our @Configuration classes without issue, but we use application.properties. Have you tried setting it there? Or maybe on command line?






            share|improve this answer






























              -1














              I believe that your configuration class needs getters and setters for a private field annotated with @Value, in order for Spring to be able to set the field!






              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%2f53279430%2faccessing-spring-application-name-in-custom-starter%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                I had similar issue in past and I solved by autowiring org.springframework.core.env.Environment; something like this:



                @Configuration
                public class CustomAutoConfiguration
                @Autowired
                private Evinronment env;
                private String appName;
                @PostConstruct
                public void initialize()
                this.appName = env.getProperty("spring.application.name");





                Not tested but it should work



                Angelo






                share|improve this answer























                • PostConstruct never gets called in my case.

                  – nixgadgets
                  Nov 13 '18 at 12:59















                1














                I had similar issue in past and I solved by autowiring org.springframework.core.env.Environment; something like this:



                @Configuration
                public class CustomAutoConfiguration
                @Autowired
                private Evinronment env;
                private String appName;
                @PostConstruct
                public void initialize()
                this.appName = env.getProperty("spring.application.name");





                Not tested but it should work



                Angelo






                share|improve this answer























                • PostConstruct never gets called in my case.

                  – nixgadgets
                  Nov 13 '18 at 12:59













                1












                1








                1







                I had similar issue in past and I solved by autowiring org.springframework.core.env.Environment; something like this:



                @Configuration
                public class CustomAutoConfiguration
                @Autowired
                private Evinronment env;
                private String appName;
                @PostConstruct
                public void initialize()
                this.appName = env.getProperty("spring.application.name");





                Not tested but it should work



                Angelo






                share|improve this answer













                I had similar issue in past and I solved by autowiring org.springframework.core.env.Environment; something like this:



                @Configuration
                public class CustomAutoConfiguration
                @Autowired
                private Evinronment env;
                private String appName;
                @PostConstruct
                public void initialize()
                this.appName = env.getProperty("spring.application.name");





                Not tested but it should work



                Angelo







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 11:05









                Angelo ImmediataAngelo Immediata

                4,36641638




                4,36641638












                • PostConstruct never gets called in my case.

                  – nixgadgets
                  Nov 13 '18 at 12:59

















                • PostConstruct never gets called in my case.

                  – nixgadgets
                  Nov 13 '18 at 12:59
















                PostConstruct never gets called in my case.

                – nixgadgets
                Nov 13 '18 at 12:59





                PostConstruct never gets called in my case.

                – nixgadgets
                Nov 13 '18 at 12:59













                1














                This is what worked in the end.



                @Configuration
                public class CustomAutoConfiguration implements EnvironmentAware

                @Override
                public void setEnvironment(Environment environment)
                this.environment = environment;


                // And then accessing via this.environment.getProperty("spring.application.name")







                share|improve this answer



























                  1














                  This is what worked in the end.



                  @Configuration
                  public class CustomAutoConfiguration implements EnvironmentAware

                  @Override
                  public void setEnvironment(Environment environment)
                  this.environment = environment;


                  // And then accessing via this.environment.getProperty("spring.application.name")







                  share|improve this answer

























                    1












                    1








                    1







                    This is what worked in the end.



                    @Configuration
                    public class CustomAutoConfiguration implements EnvironmentAware

                    @Override
                    public void setEnvironment(Environment environment)
                    this.environment = environment;


                    // And then accessing via this.environment.getProperty("spring.application.name")







                    share|improve this answer













                    This is what worked in the end.



                    @Configuration
                    public class CustomAutoConfiguration implements EnvironmentAware

                    @Override
                    public void setEnvironment(Environment environment)
                    this.environment = environment;


                    // And then accessing via this.environment.getProperty("spring.application.name")








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 14:47









                    nixgadgetsnixgadgets

                    2,35484271




                    2,35484271





















                        0














                        Might have something to do with the order in which bootstrap.yml gets loaded. We uaw @Value all the time in our @Configuration classes without issue, but we use application.properties. Have you tried setting it there? Or maybe on command line?






                        share|improve this answer



























                          0














                          Might have something to do with the order in which bootstrap.yml gets loaded. We uaw @Value all the time in our @Configuration classes without issue, but we use application.properties. Have you tried setting it there? Or maybe on command line?






                          share|improve this answer

























                            0












                            0








                            0







                            Might have something to do with the order in which bootstrap.yml gets loaded. We uaw @Value all the time in our @Configuration classes without issue, but we use application.properties. Have you tried setting it there? Or maybe on command line?






                            share|improve this answer













                            Might have something to do with the order in which bootstrap.yml gets loaded. We uaw @Value all the time in our @Configuration classes without issue, but we use application.properties. Have you tried setting it there? Or maybe on command line?







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 13:46









                            MikeMike

                            1,372718




                            1,372718





















                                -1














                                I believe that your configuration class needs getters and setters for a private field annotated with @Value, in order for Spring to be able to set the field!






                                share|improve this answer



























                                  -1














                                  I believe that your configuration class needs getters and setters for a private field annotated with @Value, in order for Spring to be able to set the field!






                                  share|improve this answer

























                                    -1












                                    -1








                                    -1







                                    I believe that your configuration class needs getters and setters for a private field annotated with @Value, in order for Spring to be able to set the field!






                                    share|improve this answer













                                    I believe that your configuration class needs getters and setters for a private field annotated with @Value, in order for Spring to be able to set the field!







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 13 '18 at 13:29









                                    Ben R.Ben R.

                                    493210




                                    493210



























                                        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%2f53279430%2faccessing-spring-application-name-in-custom-starter%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

                                        Kleinkühnau

                                        Makov (Slowakei)

                                        Deutsches Schauspielhaus