Results 1 to 5 of 5

Thread: Setting property via @Value

  1. #1
    Join Date
    Feb 2010
    Posts
    7

    Default 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. #2
    Join Date
    Feb 2010
    Posts
    7

    Default

    Does anybody know?

  3. #3
    Join Date
    Feb 2010
    Posts
    7

    Default

    Ok,

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

  4. #4

    Default

    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.
    -Amit

  5. #5
    Join Date
    Dec 2007
    Location
    Boston, MA
    Posts
    34

    Default

    Quote 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •