0

( This is not a duplicate question, There is nothing about the @inject annotation here : Dependency injection through constructors or property setters? )


If I understand it right, in Spring for example, we define a bean in the xml file, and then we can inject it via constructor or setter, so the Spring IOC container will manage the lifecycle of that bean.

Example 1 :

class Test {

    private Bean bean;

    //constructor injectioon
    public Test(Bean bean) {
        this.bean = bean;
    }
    //setter injection
    public setBean(Bean bean) {
        this.bean = bean;
    }
}

So what is the difference between the example 1 and the following example ( and correct me please if I'm wrong about what I've said in the first paragraph :

Example 2 :

class Test {

    @inject
    private Bean bean;

    public Test() {

    }
}

Thanks in advance.

Community
  • 1
  • 1
Dwix
  • 1,139
  • 3
  • 20
  • 45

0 Answers0