在我的spring示例中,我使用以下XML配置文件声明了两个bean。
EmployeeBean.java
package autowire;
import org.springframework.beans.factory.annotation.Autowired;
public class EmployeeBean {
private String fullName;
@Autowired
private DepartmentBean departmentBean;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public DepartmentBean getDepartmentBean() {
return departmentBean;
}
}
DepartmentBean.java
package autowire;
public class DepartmentBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
弹簧servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config />
<bean id="employee" class="autowire.EmployeeBean" autowire="byType">
<property name="fullName" value="Charith"></property>
</bean>
<bean id="deptment" class="autowire.DepartmentBean">
<property name="name" value="IT Department"></property>
</bean>
</beans>
TestAutowire .java
package autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAutowire {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"spring-servlet.xml"});
EmployeeBean employee = (EmployeeBean)context.getBean("employee");
System.out.println(employee.getFullName());
System.out.println(employee.getDepartmentBean().getName());
}
}
上面的例子很好。之后我删除了'@Autowired'注释并将以下行添加到EmployeeBean.java
public void setDepartmentBean(DepartmentBean departmentBean) {
this.departmentBean = departmentBean;
}
现在这个例子工作得很好,输出相同。我的问题是,使用'@Autowired'注释时的实际好处是什么?因为代码工作正常,没有注释,但也有setter方法。请帮助我。
答案 0 :(得分:1)
@AutoWired
非常有用,因为它可以节省您编写“布线”代码的时间。您不必在代码中的某处调用setDepartment
方法来初始化对象。春天会为你做的。
在您的情况下,请参阅下面如何仅使用注释来完成此操作。请注意使用@Component
注释指示Spring的自动扫描组件。另请注意,现在不需要XML文件。
<强> EmployeeBean.java 强>
package autowire;
/* Imports go here ... */
@Component
public class EmployeeBean {
private String fullName;
@Autowired
private DepartmentBean departmentBean;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public DepartmentBean getDepartmentBean() {
return departmentBean;
}
}
<强> DepartmentBean.java 强>
package autowire;
/* Imports go here ... */
@Component
public class DepartmentBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
TestAutowire .java
package autowire;
/* Imports go here ... */
public class TestAutowire {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("autowire");
context.refresh();
EmployeeBean employee = (EmployeeBean)context.getBean("employee");
System.out.println(employee.getFullName());
System.out.println(employee.getDepartmentBean().getName());
}
}
参考文献: