Springboot - @Autowired null with injection












0















I have a Spring-boot project and I need to get values from application.properties file because of profiles.



But when injecting a config file in the class, the object return null.



Application.properties:



server.ip=000.000.000.000
server.port=0000


Config Class:



@Component
@ConfigurationProperties(prefix = "server")
public class AppProperties {

private String ip;
private Integer port;

... getters and setters


the class where I need the values:



@Component
public class Teste {

@Autowired
private AppProperties properties;

...
socket = new Socket(properties.getIp(), properties.getPort());


On debugging, at the start of the application, the values in AppProperties get a correct value.










share|improve this question

























  • And you got NPE where exaclty?

    – Antoniossss
    Jan 19 at 12:18






  • 2





    For me your code works. Do you have the spring-boot-configuration-processor dependency included in your build file? STS warns about this on @ConfigurationProperties when it is missing.

    – Thomas Fritsch
    Jan 19 at 12:26











  • i nedd get values from application.properties file because of profiles. Umm i don't get why would you do this "because of profiles". Both things exists and can be used separately.

    – Antoniossss
    Jan 19 at 12:48













  • remove that @Component from AppProperties this should be sufficient @ConfigurationProperties(prefix = "server")

    – Deadpool
    Jan 19 at 15:33











  • Nullpointer in variable "properties" on start socket. I have spring-boot-configuration-processor dependency. I need use profiles because different environments. I have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly in all environments. The properties "ip" and "port" exists in all application.properties files.

    – RafaelE
    Jan 20 at 4:38
















0















I have a Spring-boot project and I need to get values from application.properties file because of profiles.



But when injecting a config file in the class, the object return null.



Application.properties:



server.ip=000.000.000.000
server.port=0000


Config Class:



@Component
@ConfigurationProperties(prefix = "server")
public class AppProperties {

private String ip;
private Integer port;

... getters and setters


the class where I need the values:



@Component
public class Teste {

@Autowired
private AppProperties properties;

...
socket = new Socket(properties.getIp(), properties.getPort());


On debugging, at the start of the application, the values in AppProperties get a correct value.










share|improve this question

























  • And you got NPE where exaclty?

    – Antoniossss
    Jan 19 at 12:18






  • 2





    For me your code works. Do you have the spring-boot-configuration-processor dependency included in your build file? STS warns about this on @ConfigurationProperties when it is missing.

    – Thomas Fritsch
    Jan 19 at 12:26











  • i nedd get values from application.properties file because of profiles. Umm i don't get why would you do this "because of profiles". Both things exists and can be used separately.

    – Antoniossss
    Jan 19 at 12:48













  • remove that @Component from AppProperties this should be sufficient @ConfigurationProperties(prefix = "server")

    – Deadpool
    Jan 19 at 15:33











  • Nullpointer in variable "properties" on start socket. I have spring-boot-configuration-processor dependency. I need use profiles because different environments. I have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly in all environments. The properties "ip" and "port" exists in all application.properties files.

    – RafaelE
    Jan 20 at 4:38














0












0








0


0






I have a Spring-boot project and I need to get values from application.properties file because of profiles.



But when injecting a config file in the class, the object return null.



Application.properties:



server.ip=000.000.000.000
server.port=0000


Config Class:



@Component
@ConfigurationProperties(prefix = "server")
public class AppProperties {

private String ip;
private Integer port;

... getters and setters


the class where I need the values:



@Component
public class Teste {

@Autowired
private AppProperties properties;

...
socket = new Socket(properties.getIp(), properties.getPort());


On debugging, at the start of the application, the values in AppProperties get a correct value.










share|improve this question
















I have a Spring-boot project and I need to get values from application.properties file because of profiles.



But when injecting a config file in the class, the object return null.



Application.properties:



server.ip=000.000.000.000
server.port=0000


Config Class:



@Component
@ConfigurationProperties(prefix = "server")
public class AppProperties {

private String ip;
private Integer port;

... getters and setters


the class where I need the values:



@Component
public class Teste {

@Autowired
private AppProperties properties;

...
socket = new Socket(properties.getIp(), properties.getPort());


On debugging, at the start of the application, the values in AppProperties get a correct value.







java spring spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 12:49









Ompratap Singh

349




349










asked Jan 19 at 11:55









RafaelERafaelE

12




12













  • And you got NPE where exaclty?

    – Antoniossss
    Jan 19 at 12:18






  • 2





    For me your code works. Do you have the spring-boot-configuration-processor dependency included in your build file? STS warns about this on @ConfigurationProperties when it is missing.

    – Thomas Fritsch
    Jan 19 at 12:26











  • i nedd get values from application.properties file because of profiles. Umm i don't get why would you do this "because of profiles". Both things exists and can be used separately.

    – Antoniossss
    Jan 19 at 12:48













  • remove that @Component from AppProperties this should be sufficient @ConfigurationProperties(prefix = "server")

    – Deadpool
    Jan 19 at 15:33











  • Nullpointer in variable "properties" on start socket. I have spring-boot-configuration-processor dependency. I need use profiles because different environments. I have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly in all environments. The properties "ip" and "port" exists in all application.properties files.

    – RafaelE
    Jan 20 at 4:38



















  • And you got NPE where exaclty?

    – Antoniossss
    Jan 19 at 12:18






  • 2





    For me your code works. Do you have the spring-boot-configuration-processor dependency included in your build file? STS warns about this on @ConfigurationProperties when it is missing.

    – Thomas Fritsch
    Jan 19 at 12:26











  • i nedd get values from application.properties file because of profiles. Umm i don't get why would you do this "because of profiles". Both things exists and can be used separately.

    – Antoniossss
    Jan 19 at 12:48













  • remove that @Component from AppProperties this should be sufficient @ConfigurationProperties(prefix = "server")

    – Deadpool
    Jan 19 at 15:33











  • Nullpointer in variable "properties" on start socket. I have spring-boot-configuration-processor dependency. I need use profiles because different environments. I have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly in all environments. The properties "ip" and "port" exists in all application.properties files.

    – RafaelE
    Jan 20 at 4:38

















And you got NPE where exaclty?

– Antoniossss
Jan 19 at 12:18





And you got NPE where exaclty?

– Antoniossss
Jan 19 at 12:18




2




2





For me your code works. Do you have the spring-boot-configuration-processor dependency included in your build file? STS warns about this on @ConfigurationProperties when it is missing.

– Thomas Fritsch
Jan 19 at 12:26





For me your code works. Do you have the spring-boot-configuration-processor dependency included in your build file? STS warns about this on @ConfigurationProperties when it is missing.

– Thomas Fritsch
Jan 19 at 12:26













i nedd get values from application.properties file because of profiles. Umm i don't get why would you do this "because of profiles". Both things exists and can be used separately.

– Antoniossss
Jan 19 at 12:48







i nedd get values from application.properties file because of profiles. Umm i don't get why would you do this "because of profiles". Both things exists and can be used separately.

– Antoniossss
Jan 19 at 12:48















remove that @Component from AppProperties this should be sufficient @ConfigurationProperties(prefix = "server")

– Deadpool
Jan 19 at 15:33





remove that @Component from AppProperties this should be sufficient @ConfigurationProperties(prefix = "server")

– Deadpool
Jan 19 at 15:33













Nullpointer in variable "properties" on start socket. I have spring-boot-configuration-processor dependency. I need use profiles because different environments. I have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly in all environments. The properties "ip" and "port" exists in all application.properties files.

– RafaelE
Jan 20 at 4:38





Nullpointer in variable "properties" on start socket. I have spring-boot-configuration-processor dependency. I need use profiles because different environments. I have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly in all environments. The properties "ip" and "port" exists in all application.properties files.

– RafaelE
Jan 20 at 4:38












3 Answers
3






active

oldest

votes


















2














You can use @Value annotation to access the property values from application.properties file like this



@Value("${<propertname>}")
private String userBucketPath;





share|improve this answer



















  • 1





    Nope @ConfigurationProperties are to map props into objects directly.

    – Antoniossss
    Jan 19 at 12:35













  • Yes I accept, But this can access individual properties value and it also may be useful.

    – venkat
    Jan 19 at 12:39











  • Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

    – Antoniossss
    Jan 19 at 12:46













  • Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

    – RafaelE
    Jan 20 at 4:25













  • My best bet is that OP is doing something like new Teste().

    – Antoniossss
    Jan 20 at 15:01



















0














Your code looks like correct but I would like to suggest you try with below steps also:




  1. Use @PropertySource annotation to define properties location. (I knew default location is classpath).


  2. If you really need to add a profile to your project. Create a new application property file like application-{profile-name}.properties and while starting an application pass JVM argument -Dspring.profiles.active={profile-name}.



I don't know it's exact need or answer for you. I'm just trying to share an additional step.






share|improve this answer
























  • Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

    – RafaelE
    Jan 20 at 4:15





















-1














I get null values for properties because I instantiate a Teste class with new Teste().



The Solution is use Teste class with Intection.



@Autowired
private Teste teste;





share|improve this answer


























  • Thats what I said. Its wrong

    – Antoniossss
    Jan 20 at 19:53













  • Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

    – RafaelE
    Jan 20 at 20:11











  • Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

    – Antoniossss
    Jan 20 at 20:18













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%2f54266814%2fspringboot-autowired-null-with-injection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use @Value annotation to access the property values from application.properties file like this



@Value("${<propertname>}")
private String userBucketPath;





share|improve this answer



















  • 1





    Nope @ConfigurationProperties are to map props into objects directly.

    – Antoniossss
    Jan 19 at 12:35













  • Yes I accept, But this can access individual properties value and it also may be useful.

    – venkat
    Jan 19 at 12:39











  • Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

    – Antoniossss
    Jan 19 at 12:46













  • Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

    – RafaelE
    Jan 20 at 4:25













  • My best bet is that OP is doing something like new Teste().

    – Antoniossss
    Jan 20 at 15:01
















2














You can use @Value annotation to access the property values from application.properties file like this



@Value("${<propertname>}")
private String userBucketPath;





share|improve this answer



















  • 1





    Nope @ConfigurationProperties are to map props into objects directly.

    – Antoniossss
    Jan 19 at 12:35













  • Yes I accept, But this can access individual properties value and it also may be useful.

    – venkat
    Jan 19 at 12:39











  • Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

    – Antoniossss
    Jan 19 at 12:46













  • Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

    – RafaelE
    Jan 20 at 4:25













  • My best bet is that OP is doing something like new Teste().

    – Antoniossss
    Jan 20 at 15:01














2












2








2







You can use @Value annotation to access the property values from application.properties file like this



@Value("${<propertname>}")
private String userBucketPath;





share|improve this answer













You can use @Value annotation to access the property values from application.properties file like this



@Value("${<propertname>}")
private String userBucketPath;






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 12:31









venkatvenkat

18819




18819








  • 1





    Nope @ConfigurationProperties are to map props into objects directly.

    – Antoniossss
    Jan 19 at 12:35













  • Yes I accept, But this can access individual properties value and it also may be useful.

    – venkat
    Jan 19 at 12:39











  • Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

    – Antoniossss
    Jan 19 at 12:46













  • Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

    – RafaelE
    Jan 20 at 4:25













  • My best bet is that OP is doing something like new Teste().

    – Antoniossss
    Jan 20 at 15:01














  • 1





    Nope @ConfigurationProperties are to map props into objects directly.

    – Antoniossss
    Jan 19 at 12:35













  • Yes I accept, But this can access individual properties value and it also may be useful.

    – venkat
    Jan 19 at 12:39











  • Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

    – Antoniossss
    Jan 19 at 12:46













  • Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

    – RafaelE
    Jan 20 at 4:25













  • My best bet is that OP is doing something like new Teste().

    – Antoniossss
    Jan 20 at 15:01








1




1





Nope @ConfigurationProperties are to map props into objects directly.

– Antoniossss
Jan 19 at 12:35







Nope @ConfigurationProperties are to map props into objects directly.

– Antoniossss
Jan 19 at 12:35















Yes I accept, But this can access individual properties value and it also may be useful.

– venkat
Jan 19 at 12:39





Yes I accept, But this can access individual properties value and it also may be useful.

– venkat
Jan 19 at 12:39













Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

– Antoniossss
Jan 19 at 12:46







Well if you rly want to go this way, lets paste link to spring documentation as whats in it "might be usefull as well". Focus on given scenario.

– Antoniossss
Jan 19 at 12:46















Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

– RafaelE
Jan 20 at 4:25







Thank's for your answer. No success using @Value annotation: @Value("${server.ip}")

– RafaelE
Jan 20 at 4:25















My best bet is that OP is doing something like new Teste().

– Antoniossss
Jan 20 at 15:01





My best bet is that OP is doing something like new Teste().

– Antoniossss
Jan 20 at 15:01













0














Your code looks like correct but I would like to suggest you try with below steps also:




  1. Use @PropertySource annotation to define properties location. (I knew default location is classpath).


  2. If you really need to add a profile to your project. Create a new application property file like application-{profile-name}.properties and while starting an application pass JVM argument -Dspring.profiles.active={profile-name}.



I don't know it's exact need or answer for you. I'm just trying to share an additional step.






share|improve this answer
























  • Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

    – RafaelE
    Jan 20 at 4:15


















0














Your code looks like correct but I would like to suggest you try with below steps also:




  1. Use @PropertySource annotation to define properties location. (I knew default location is classpath).


  2. If you really need to add a profile to your project. Create a new application property file like application-{profile-name}.properties and while starting an application pass JVM argument -Dspring.profiles.active={profile-name}.



I don't know it's exact need or answer for you. I'm just trying to share an additional step.






share|improve this answer
























  • Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

    – RafaelE
    Jan 20 at 4:15
















0












0








0







Your code looks like correct but I would like to suggest you try with below steps also:




  1. Use @PropertySource annotation to define properties location. (I knew default location is classpath).


  2. If you really need to add a profile to your project. Create a new application property file like application-{profile-name}.properties and while starting an application pass JVM argument -Dspring.profiles.active={profile-name}.



I don't know it's exact need or answer for you. I'm just trying to share an additional step.






share|improve this answer













Your code looks like correct but I would like to suggest you try with below steps also:




  1. Use @PropertySource annotation to define properties location. (I knew default location is classpath).


  2. If you really need to add a profile to your project. Create a new application property file like application-{profile-name}.properties and while starting an application pass JVM argument -Dspring.profiles.active={profile-name}.



I don't know it's exact need or answer for you. I'm just trying to share an additional step.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 12:20









Ompratap SinghOmpratap Singh

349




349













  • Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

    – RafaelE
    Jan 20 at 4:15





















  • Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

    – RafaelE
    Jan 20 at 4:15



















Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

– RafaelE
Jan 20 at 4:15







Thank's for your answer. I Have 3 configuration files: application.properties, application-pro.properties and application-dev.properties. The configurations about database work correctly. No succes using @PropertySource annotation

– RafaelE
Jan 20 at 4:15













-1














I get null values for properties because I instantiate a Teste class with new Teste().



The Solution is use Teste class with Intection.



@Autowired
private Teste teste;





share|improve this answer


























  • Thats what I said. Its wrong

    – Antoniossss
    Jan 20 at 19:53













  • Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

    – RafaelE
    Jan 20 at 20:11











  • Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

    – Antoniossss
    Jan 20 at 20:18


















-1














I get null values for properties because I instantiate a Teste class with new Teste().



The Solution is use Teste class with Intection.



@Autowired
private Teste teste;





share|improve this answer


























  • Thats what I said. Its wrong

    – Antoniossss
    Jan 20 at 19:53













  • Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

    – RafaelE
    Jan 20 at 20:11











  • Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

    – Antoniossss
    Jan 20 at 20:18
















-1












-1








-1







I get null values for properties because I instantiate a Teste class with new Teste().



The Solution is use Teste class with Intection.



@Autowired
private Teste teste;





share|improve this answer















I get null values for properties because I instantiate a Teste class with new Teste().



The Solution is use Teste class with Intection.



@Autowired
private Teste teste;






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 21 at 23:20

























answered Jan 20 at 19:51









RafaelERafaelE

12




12













  • Thats what I said. Its wrong

    – Antoniossss
    Jan 20 at 19:53













  • Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

    – RafaelE
    Jan 20 at 20:11











  • Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

    – Antoniossss
    Jan 20 at 20:18





















  • Thats what I said. Its wrong

    – Antoniossss
    Jan 20 at 19:53













  • Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

    – RafaelE
    Jan 20 at 20:11











  • Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

    – Antoniossss
    Jan 20 at 20:18



















Thats what I said. Its wrong

– Antoniossss
Jan 20 at 19:53







Thats what I said. Its wrong

– Antoniossss
Jan 20 at 19:53















Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

– RafaelE
Jan 20 at 20:11





Yes. I create a new answer to facilitate the search of other users with the same problem. it's wrong? This is my first question on Stackoverflow. Excuse me.

– RafaelE
Jan 20 at 20:11













Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

– Antoniossss
Jan 20 at 20:18







Right now its super mess. Moreover you accepted answer that is not an answer so whats the point of create a new answer to facilitate the search of other users with the same problem. if it is not considered solution here?

– Antoniossss
Jan 20 at 20:18




















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%2f54266814%2fspringboot-autowired-null-with-injection%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

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage