Accessing spring application name in custom starter
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
add a comment |
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
add a comment |
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
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
java spring spring-boot
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
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
PostConstructnever gets called in my case.
– nixgadgets
Nov 13 '18 at 12:59
add a comment |
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")
add a comment |
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?
add a comment |
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!
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
PostConstructnever gets called in my case.
– nixgadgets
Nov 13 '18 at 12:59
add a comment |
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
PostConstructnever gets called in my case.
– nixgadgets
Nov 13 '18 at 12:59
add a comment |
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
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
answered Nov 13 '18 at 11:05
Angelo ImmediataAngelo Immediata
4,36641638
4,36641638
PostConstructnever gets called in my case.
– nixgadgets
Nov 13 '18 at 12:59
add a comment |
PostConstructnever 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
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
answered Nov 13 '18 at 14:47
nixgadgetsnixgadgets
2,35484271
2,35484271
add a comment |
add a comment |
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?
add a comment |
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?
add a comment |
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?
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?
answered Nov 13 '18 at 13:46
MikeMike
1,372718
1,372718
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Nov 13 '18 at 13:29
Ben R.Ben R.
493210
493210
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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