I'm newbie of Spring, I try to understand how @Autowired
annotation works with a struts2 action. This is my scenario:
UserBean.java
public class UserBean {
private String userName;
private int userAge;
private String userGender;
private String userJob;
private String[] userHobbies;
/*Getters and Setters */
}
UserAction.java
@Component
public class UserAction extends ActionSupport implements ModelDriven<UserBean> {
@Autowired
private UserBean userBean;
public String execute() {
return SUCCESS;
}
public String addUser() {
return SUCCESS;
}
public UserBean getModel() {
return userBean;
}
public UserBean getUserBean() {
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
}
applicationContext.xml
<context:annotation-config />
<context:component-scan base-package="com.gmail.amato.giorgio.*" />
<bean id="userAction" class="com.gmail.amato.giorgio.UserAction"></bean>
<bean id="userBean" class="com.gmail.amato.giorgio.UserBean"></bean>
Now my program is fine, and I don't have any error: I can see a form, fill that and see a result back to me.
My question is: If I using @Autowired
annotation, why I have to write bean id for the userBean? It should be injecting automatically by the Spring Container?
What is the advantage using @Autowired
annotation if I have still to write both bean definition in my applicationContext.xml
?