Announcement Announcement Module
Collapse
No announcement yet.
Setting property via @Value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting property via @Value

    Hi,

    I've tried set the property in the following way:

    @Component
    public class Empl {

    private int age;

    public Empl() {}

    public Empl( @Value("4") int age ) {
    this.age = age;
    }

    }

    It doesn' work. Where am I wrong?

  • #2
    Does anybody know?

    Comment


    • #3
      Ok,

      I know now. You should use @Autowired to indicate a constructor to create bean

      Comment


      • #4
        Because you had a default constructor on the Empl class. That is the one that would get called by default by the Spring container.
        Which is why you had to add @Autowired for the non default constructor.

        The thing that I'm not sure about is if you did not have a default constructor would Spring container have called the non-default one?
        My guess is probably not, and it would have thrown an exception- no default constructor found.

        Comment


        • #5
          Originally posted by amit.kapps View Post
          The thing that I'm not sure about is if you did not have a default constructor would Spring container have called the non-default one?
          My guess is probably not, and it would have thrown an exception- no default constructor found.
          No. Once you have @Autowired, spring will use the non-default contructor to instantiate the bean. It works this way for me. Comment out your default constructor and try it.

          Comment

          Working...
          X