Java Spring依赖项注入在调用其类方法时获得NullPointException
我有以下Java类,它有一个字段Java Spring依赖项注入在调用其类方法时获得NullPointException,java,spring,dependency-injection,nullpointerexception,Java,Spring,Dependency Injection,Nullpointerexception,我有以下Java类,它有一个字段dao,由构造函数args注入: public class FeatureFormationFromRaw { private MyDAOImpl dao; //field to be injected from beam.xml public FeatureFormationFromRaw(MyDAOImpl dao) { //do a fresh clean and save all data, dependency i
dao
,由构造函数args注入:
public class FeatureFormationFromRaw {
private MyDAOImpl dao; //field to be injected from beam.xml
public FeatureFormationFromRaw(MyDAOImpl dao) {
//do a fresh clean and save all data, dependency injection by constructor args
dao.removeAll(); //This is fine.
dao.saveDataAll(); // This is fine.
}
public float calcuFeatures(String caseName) {
List<JSONObject> rawData = dao.getData(caseName); //This line throws NullPointException because dao=null here.
.........
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
FeatureFormationFromRaw featureForm = (FeatureFormationFromRaw) context.getBean("featureFormation");
float value = featureForm.calcuFeatures("000034");
}
}
<bean id="featureFormation" class="com.example.FeatureFormationFromRaw">
<constructor-arg ref="myDaoImpl" />
</bean>
<bean id="myDaoImpl" class="com.example.MyDAOImpl">
</bean>
我调试了我的应用程序,发现当构造函数
FeatureFormationFromRaw(MyDAOImpl dao)
执行时,dao
从Springbean注入中获得正确的值。但是,当调用方法calcuFeatures()
时,变量dao
在第一行为null。为什么呢?为什么变量dao
在构造函数调用后消失并变为null?在构造函数中,传入dao后,必须将dao分配给私有变量。否则你就不能在其他地方叫它了
添加this.dao=dao代码>到您的构造函数
换句话说,当您在构造函数中调用dao.removeAll()
时,这会起作用,因为它使用的是参数dao
。但是当您在另一个方法中调用dao.getData()
时,它会失败,因为它使用的是private MyDAOImpl dao代码>尚未初始化。注入将其放入构造函数,但不将其放入私有变量。必须这样做。在构造函数中,传入dao后,必须将dao分配给私有变量。否则你就不能在其他地方叫它了
private MyDAOImpl dao; //field to be injected from beam.xml
public FeatureFormationFromRaw(MyDAOImpl dao) {
//do a fresh clean and save all data, dependency injection by constructor args
dao.removeAll(); //This is fine.
dao.saveDataAll(); // This is fine.
}
添加this.dao=dao代码>到您的构造函数
换句话说,当您在构造函数中调用dao.removeAll()
时,这会起作用,因为它使用的是参数dao
。但是当您在另一个方法中调用dao.getData()
时,它会失败,因为它使用的是private MyDAOImpl dao代码>尚未初始化。注入将其放入构造函数,但不将其放入私有变量。你必须这么做
private MyDAOImpl dao; //field to be injected from beam.xml
public FeatureFormationFromRaw(MyDAOImpl dao) {
//do a fresh clean and save all data, dependency injection by constructor args
dao.removeAll(); //This is fine.
dao.saveDataAll(); // This is fine.
}
添加this.dao=dao代码>在构造函数中..尚未分配,因此当您与其他方法一起使用时,它的null
将以NPE
结束
添加this.dao=dao代码>在你的构造函数中..它还没有被赋值,所以当你与其他方法一起使用时,它的null
所以你结束了与NPE
你是否设置了私有的MyDAOImpl dao;这在你的构造函数中..即this.dao=dao;您没有在构造函数中分配变量。从名称来看,您应该编程到接口MyDAO
而不是实现MyDAOImpl
@M.Deinum:谢谢您的建议。我可以在构造函数arg中使用MyDAO
而不是实现类MyDAOImpl
,但是我需要执行类似this.dao=(MyDAOImpl)dao
的强制转换。比起在构造函数参数中实现类,使用接口总是一种好的做法吗?关于在Spring DI构造函数args中使用接口与具体类的任何指导原则?理想情况下,您应该为构造函数arg和实际变量使用接口。通过这种方式,您可以获得编码到接口和依赖项注入的所有好处(能够隐藏实现细节,能够轻松测试)。@JacobM:谢谢您的评论。这让我想起了OO的基本原理:)。您是否设置了私有MyDAOImpl-dao;这在你的构造函数中..即this.dao=dao;您没有在构造函数中分配变量。从名称来看,您应该编程到接口MyDAO
而不是实现MyDAOImpl
@M.Deinum:谢谢您的建议。我可以在构造函数arg中使用MyDAO
而不是实现类MyDAOImpl
,但是我需要执行类似this.dao=(MyDAOImpl)dao
的强制转换。比起在构造函数参数中实现类,使用接口总是一种好的做法吗?关于在Spring DI构造函数args中使用接口与具体类的任何指导原则?理想情况下,您应该为构造函数arg和实际变量使用接口。通过这种方式,您可以获得编码到接口和依赖项注入的所有好处(能够隐藏实现细节,能够轻松测试)。@JacobM:谢谢您的评论。这让我想起了OO的基本原理:)。谢谢。这就是导致NullPointException的原因。当我习惯了通过设置方法进行DI后,我忘记了这一行。谢谢。这就是导致NullPointException的原因。在我习惯了通过设置方法进行DI之后,我忘记了这一行。