在我的 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;
}
}
spring-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 方法。请帮助我。
最佳答案
@AutoWired
很有用,因为它可以节省您编写“接线”代码的时间。您不必在代码中的某个位置调用 setDepartment
方法来初始化对象。 Spring 会为你做到这一点。
根据您的情况,请参阅下文如何仅使用注释来完成此操作。请注意使用 @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());
}
}
引用文献:
关于java - @Autowired注解有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780188/