Spring 当getter方法返回与属性不同的类型时,为什么xml autowire注入失败?
我编写了一段代码来测试autowire xml配置。但是我一直都在得到null指针异常,这表明字段自动连接失败 与其他一些可以工作的示例相反,我发现getter方法返回的类型与要连接的字段不同。删除这个getter方法或修改返回类型,它就会工作。 但我不知道原因。为什么getter方法很重要?我认为setter方法是用来连接的,那么getter方法呢Spring 当getter方法返回与属性不同的类型时,为什么xml autowire注入失败?,spring,autowired,getter-setter,Spring,Autowired,Getter Setter,我编写了一段代码来测试autowire xml配置。但是我一直都在得到null指针异常,这表明字段自动连接失败 与其他一些可以工作的示例相反,我发现getter方法返回的类型与要连接的字段不同。删除这个getter方法或修改返回类型,它就会工作。 但我不知道原因。为什么getter方法很重要?我认为setter方法是用来连接的,那么getter方法呢 public class Department { private String departName; public Str
public class Department {
private String departName;
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
}
public class Employee {
private Department department;
// 1) below code will cause NPE
public String getDepartment() {
return department.getDepartName();
}
// 2)below code works, or just delete 1) code works as well
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public void foo() {
System.out.println(department.getDepartName());
}
}
autowire xml配置文件如下所示:
<bean id="depart" class="com.kingdom.Department">
<property name="departName" value="risk"/>
</bean>
<bean id="employee" class="com.kingdom.Employee" autowire="byType">
</bean>
我希望部门可以自动连接到员工。但我得了NPE
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:294)
at java.lang.Thread.run (Thread.java:748)
Caused by: java.lang.NullPointerException
at com.kingdom.Employee.foo (Employee.java:17)
at com.kingdom.Main.main (Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:294)
at java.lang.Thread.run (Thread.java:748)
修改返回类型后,它将正常工作
但是有人知道后面发生了什么吗?为什么getter方法会破坏自动连线?要使用自动连线,必须在beans.xml中包含它。您还可以通过包括来启用它,这将隐式启用的功能 从Spring4.3开始,如果目标bean只有一个构造函数,@Autowired不再需要在目标bean上显式注释。但是,如果有多个构造函数可用,则必须存在@Autowired来教容器如何注入bean 因此,这意味着如果您不想在Employee上注释任何@Autowired,则必须使Employee只有一个构造函数:
public class Employee {
public Employee(Department department) {
this.department = department;
}
}
在这两种情况下,要使自动连接生效,仍然需要使用。我认为它不需要注释配置。错了吗?谢谢。请看我的更新。如果不使用@autowired,则必须使员工只有一个构造函数。它仅在Spring 4.3之后工作。但它仍然需要注释confighi ken,正如我所说,当我将getter methode返回类型字符串修改为bean类部门时,它工作了!我关心的是为什么getter方法很重要?getter并不重要。它之所以有效,是因为由于您的配置,员工中的部门始终为空,因此部门无法注入员工。工作的getter,因为如果您不访问空部门上的任何属性。由于您访问该空部门上的某些属性,因此它具有空指针异常,因此无法工作的getter。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config>
<bean> .... </bean>
</beans>
public class Employee {
@Autowired
public void setDepartment(Department department) {
this.department = department;
}
}
public class Employee {
public Employee(Department department) {
this.department = department;
}
}