KwiwaA
KwiwaA

Reputation: 21

Difference between injection using @inject and applicationContext.xml

Is there any difference in how Spring injects the referenced bean between

MyClass.java

public class MyClass {
    @Inject
    @Named("myNiceBean")
    private MyBean bean;
    public MyBean getBean() { return bean; }
    public void setBean(MyBean bean) { this.bean = bean; }
}

ApplicationContext.xml

<bean id="myNiceBean" class="com.exemple.DummyBean" />

AND

public class MyClass {
    private MyBean bean;
    public MyBean getBean() { return bean; }
    public void setBean(MyBean bean) { this.bean = bean; }
}

ApplicationContext.xml

<bean id="myNiceBean" class="com.exemple.DummyBean" />
<bean id="myClass" class="com.exemple.MyClass">
    <property name="bean" ref="myNiceBean"/>
</bean>

Upvotes: 1

Views: 121

Answers (1)

Reimeus
Reimeus

Reputation: 159754

The first example simply uses the annotated approach to inject the bean. In fact, there is no need for the setter and getter methods, Spring will take care of this. In the second (manual) injection example, the setter & getter methods are required.

Upvotes: 1

Related Questions