using Spring property when initializing a class [duplicate]

Multi tool use
Multi tool use












0
















This question already has an answer here:




  • Spring @Autowired and @Value on property not working

    3 answers



  • Property values in bean class are null in constructor

    2 answers




The following produces a null error ("A" is null), but I am not sure why. Is the bean instantiated before the property value is set?



package org.ets.readtogether.queuing;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Abean")
public class Test {

@Value("${send.timeout.secs}")
public Integer A;

public int B = A * 1000;
}









share|improve this question













marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Well yeah, how do you set a field without there being an object?

    – Sotirios Delimanolis
    Nov 21 '18 at 14:53











  • You can also use SpEL in the @Value annotation: @Value("#{systemProperties['send.timeout.secs'] * 1000}")

    – daniu
    Nov 21 '18 at 15:07


















0
















This question already has an answer here:




  • Spring @Autowired and @Value on property not working

    3 answers



  • Property values in bean class are null in constructor

    2 answers




The following produces a null error ("A" is null), but I am not sure why. Is the bean instantiated before the property value is set?



package org.ets.readtogether.queuing;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Abean")
public class Test {

@Value("${send.timeout.secs}")
public Integer A;

public int B = A * 1000;
}









share|improve this question













marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Well yeah, how do you set a field without there being an object?

    – Sotirios Delimanolis
    Nov 21 '18 at 14:53











  • You can also use SpEL in the @Value annotation: @Value("#{systemProperties['send.timeout.secs'] * 1000}")

    – daniu
    Nov 21 '18 at 15:07
















0












0








0









This question already has an answer here:




  • Spring @Autowired and @Value on property not working

    3 answers



  • Property values in bean class are null in constructor

    2 answers




The following produces a null error ("A" is null), but I am not sure why. Is the bean instantiated before the property value is set?



package org.ets.readtogether.queuing;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Abean")
public class Test {

@Value("${send.timeout.secs}")
public Integer A;

public int B = A * 1000;
}









share|improve this question















This question already has an answer here:




  • Spring @Autowired and @Value on property not working

    3 answers



  • Property values in bean class are null in constructor

    2 answers




The following produces a null error ("A" is null), but I am not sure why. Is the bean instantiated before the property value is set?



package org.ets.readtogether.queuing;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Abean")
public class Test {

@Value("${send.timeout.secs}")
public Integer A;

public int B = A * 1000;
}




This question already has an answer here:




  • Spring @Autowired and @Value on property not working

    3 answers



  • Property values in bean class are null in constructor

    2 answers








java spring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 14:52









Mr. MundkowskyMr. Mundkowsky

424




424




marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Well yeah, how do you set a field without there being an object?

    – Sotirios Delimanolis
    Nov 21 '18 at 14:53











  • You can also use SpEL in the @Value annotation: @Value("#{systemProperties['send.timeout.secs'] * 1000}")

    – daniu
    Nov 21 '18 at 15:07





















  • Well yeah, how do you set a field without there being an object?

    – Sotirios Delimanolis
    Nov 21 '18 at 14:53











  • You can also use SpEL in the @Value annotation: @Value("#{systemProperties['send.timeout.secs'] * 1000}")

    – daniu
    Nov 21 '18 at 15:07



















Well yeah, how do you set a field without there being an object?

– Sotirios Delimanolis
Nov 21 '18 at 14:53





Well yeah, how do you set a field without there being an object?

– Sotirios Delimanolis
Nov 21 '18 at 14:53













You can also use SpEL in the @Value annotation: @Value("#{systemProperties['send.timeout.secs'] * 1000}")

– daniu
Nov 21 '18 at 15:07







You can also use SpEL in the @Value annotation: @Value("#{systemProperties['send.timeout.secs'] * 1000}")

– daniu
Nov 21 '18 at 15:07














2 Answers
2






active

oldest

votes


















1














Spring adds all the dependencies injected in an object after instantiating it. Object instantiation precedes any @Autowired dependency injection or @Value value assignment done by Spring.



The instantiation of your class's object fails even before Spring has the object to inject dependencies because the statement public int B = A * 1000; is called during object instantiation.



In order to assign the variable B a value after Spring has finished all injections, perform the operation in either a @PostConstruct method or in an @AutoWired constructor.



   public int B; // remove the assignment here.

@PostConstruct
public void postConstruct () {
this.B = A * 1000;
}


The above method would be called after the object has been instantiated and after Spring has done its work.






share|improve this answer































    1














    Try this:



    @Component("Abean")
    public class Test {

    @Value("${send.timeout.secs}")
    public Integer A;

    public int B;

    @PostConstruct
    public void init() {
    B = A * 1000;
    }
    }


    Good example here






    share|improve this answer
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Spring adds all the dependencies injected in an object after instantiating it. Object instantiation precedes any @Autowired dependency injection or @Value value assignment done by Spring.



      The instantiation of your class's object fails even before Spring has the object to inject dependencies because the statement public int B = A * 1000; is called during object instantiation.



      In order to assign the variable B a value after Spring has finished all injections, perform the operation in either a @PostConstruct method or in an @AutoWired constructor.



         public int B; // remove the assignment here.

      @PostConstruct
      public void postConstruct () {
      this.B = A * 1000;
      }


      The above method would be called after the object has been instantiated and after Spring has done its work.






      share|improve this answer




























        1














        Spring adds all the dependencies injected in an object after instantiating it. Object instantiation precedes any @Autowired dependency injection or @Value value assignment done by Spring.



        The instantiation of your class's object fails even before Spring has the object to inject dependencies because the statement public int B = A * 1000; is called during object instantiation.



        In order to assign the variable B a value after Spring has finished all injections, perform the operation in either a @PostConstruct method or in an @AutoWired constructor.



           public int B; // remove the assignment here.

        @PostConstruct
        public void postConstruct () {
        this.B = A * 1000;
        }


        The above method would be called after the object has been instantiated and after Spring has done its work.






        share|improve this answer


























          1












          1








          1







          Spring adds all the dependencies injected in an object after instantiating it. Object instantiation precedes any @Autowired dependency injection or @Value value assignment done by Spring.



          The instantiation of your class's object fails even before Spring has the object to inject dependencies because the statement public int B = A * 1000; is called during object instantiation.



          In order to assign the variable B a value after Spring has finished all injections, perform the operation in either a @PostConstruct method or in an @AutoWired constructor.



             public int B; // remove the assignment here.

          @PostConstruct
          public void postConstruct () {
          this.B = A * 1000;
          }


          The above method would be called after the object has been instantiated and after Spring has done its work.






          share|improve this answer













          Spring adds all the dependencies injected in an object after instantiating it. Object instantiation precedes any @Autowired dependency injection or @Value value assignment done by Spring.



          The instantiation of your class's object fails even before Spring has the object to inject dependencies because the statement public int B = A * 1000; is called during object instantiation.



          In order to assign the variable B a value after Spring has finished all injections, perform the operation in either a @PostConstruct method or in an @AutoWired constructor.



             public int B; // remove the assignment here.

          @PostConstruct
          public void postConstruct () {
          this.B = A * 1000;
          }


          The above method would be called after the object has been instantiated and after Spring has done its work.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 15:03









          PranjalPranjal

          1363




          1363

























              1














              Try this:



              @Component("Abean")
              public class Test {

              @Value("${send.timeout.secs}")
              public Integer A;

              public int B;

              @PostConstruct
              public void init() {
              B = A * 1000;
              }
              }


              Good example here






              share|improve this answer






























                1














                Try this:



                @Component("Abean")
                public class Test {

                @Value("${send.timeout.secs}")
                public Integer A;

                public int B;

                @PostConstruct
                public void init() {
                B = A * 1000;
                }
                }


                Good example here






                share|improve this answer




























                  1












                  1








                  1







                  Try this:



                  @Component("Abean")
                  public class Test {

                  @Value("${send.timeout.secs}")
                  public Integer A;

                  public int B;

                  @PostConstruct
                  public void init() {
                  B = A * 1000;
                  }
                  }


                  Good example here






                  share|improve this answer















                  Try this:



                  @Component("Abean")
                  public class Test {

                  @Value("${send.timeout.secs}")
                  public Integer A;

                  public int B;

                  @PostConstruct
                  public void init() {
                  B = A * 1000;
                  }
                  }


                  Good example here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 15:05

























                  answered Nov 21 '18 at 14:55









                  Essex BoyEssex Boy

                  4,7531816




                  4,7531816















                      uu fRYtITAK1F6q hT1H W1zZFmzfX,n2zfsT,8QEZn NSb,9,4MxyCyZTzfdyuUw5FKFiiUUAcXEch5fF,O1,MT
                      7vm53TThS75SnWO2BaRGTuFZf,If wFKO2kzrLeurYBkBTLvcZzve7lR

                      Popular posts from this blog

                      How to pass form data using jquery Ajax to insert data in database?

                      Guess what letter conforming each word

                      Run scheduled task as local user group (not BUILTIN)