Java 春迪的奇怪行为
我在一个类中注入依赖项。但在调用该类的方法时找不到Java 春迪的奇怪行为,java,validation,spring-mvc,dependency-injection,inversion-of-control,Java,Validation,Spring Mvc,Dependency Injection,Inversion Of Control,我在一个类中注入依赖项。但在调用该类的方法时找不到 public StudentValidator extends MyValidator implements Validator{ private StudentRepository studentRepository; //setter for the same public void validate(Object obj,Errors errors){ validateStudent(Obj) } } p
public StudentValidator extends MyValidator implements Validator{
private StudentRepository studentRepository;
//setter for the same
public void validate(Object obj,Errors errors){
validateStudent(Obj)
}
}
public class MyValidator{
private StudentRepository studentRep;
public void setStudentRep(StudentRepository studentRep){
System.out.println("This is printing on Tomcat console");
this.studentRep=studentRep
System.out.println("This is also printing"+studentRep+" with hashcode");
}
public void validateStudent(Object obj){
studentRep.findStud(); getting here NullPointerException
}
}
不需要编写SpringServlet,因为我可以看到依赖关系是通过Syso语句注入到setter中的
同样的问题是什么
更新
springservlet.xml
<beans>
<bean id="studentValidator" class="SyudentValidator" >
<property name="studentRepository" ref="studentRepository">
</bean>
<bean id="myValidator" class="MyValidator">
<property name="studentRep" ref="studentRepository">
</bean>
<bean id="studentRepository" class="StudentRepository">
</beans>
NullPointerException不是我的问题。问题是,在这种情况下,当Syso语句打印依赖项哈希代码时,为什么会得到空指针。假设MyValidator和StudentValidator都是SpringBean,您应该尝试: bean id=“myValidator”class=“myValidator.java”> bean id=“studentValidator”class=“studentValidator.java”parent=“myValidator>
您必须通知spring有一个继承。您已经声明了两次类型为
StudentRepository
的字段,这让人感到困惑,但我认为是这样的
我假设您正在创建一个StudentValidator
实例,该实例将设置字段StudentValidator.studentRepository
。但是validateStudent
方法使用字段MyValidator.studentRep
,该字段将保持null
,因此出现NullPointerException
基本上,Java中只能重写方法,不能重写字段
如果确实需要这种不必要的继承结构,则代码应为:
public class MyValidator
{
private StudentRepository studentRepository;
public void setStudentRepository(StudentRepository studentRepository)
{
this.studentRepository = studentRepository;
}
public void validateStudent(Object obj)
{
studentRepository.findStud();
}
}
public StudentValidator extends MyValidator implements Validator
{
@Override
public void validate(Object obj, Errors errors)
{
validateStudent(Obj)
}
}
<beans>
<bean id="studentValidator" class="SyudentValidator" >
<property name="studentRepository" ref="studentRepository">
<property name="studentRep" ref="studentRepository">
</bean>
<bean id="studentRepository" class="StudentRepository">
尽管我很想把它简化为:
public class StudentValidator implements Validator
{
private StudentRepository studentRepository;
public void setStudentRepository(StudentRepository studentRepository)
{
this.studentRepository = studentRepository;
}
@Override
public void validate(Object obj)
{
studentRepository.findStud();
}
}
更新
基于上面的Spring配置,我可以看到您正在创建这两个类的实例——如果您不打算使用myValidator
bean作为studentValidator
bean的父级,那么不确定为什么要这样做,但是不管怎样,如果您只有一个bean,使用父级只会使Spring的n配置复杂化哦,理性
您在validateStudent
中获得NullPointerException
的原因是,您从未在studentValidator
bean上设置studentRep
,只在myValidator
bean上设置它(它们是两个不同的实例)
鉴于此,您的Spring配置应该是:
public class MyValidator
{
private StudentRepository studentRepository;
public void setStudentRepository(StudentRepository studentRepository)
{
this.studentRepository = studentRepository;
}
public void validateStudent(Object obj)
{
studentRepository.findStud();
}
}
public StudentValidator extends MyValidator implements Validator
{
@Override
public void validate(Object obj, Errors errors)
{
validateStudent(Obj)
}
}
<beans>
<bean id="studentValidator" class="SyudentValidator" >
<property name="studentRepository" ref="studentRepository">
<property name="studentRep" ref="studentRepository">
</bean>
<bean id="studentRepository" class="StudentRepository">
尽管如我所说,您的类设计首先令人困惑,这使得这个Spring配置看起来很奇怪,因为您两次设置了对同一bean的引用,studentRepository
我的建议是应用我最初建议的类更改,这只是将设计作为一个整体,并使您更难像当前发生的那样误解您的应用程序。您省略了
studentRepository
类的StudentValidator
字段的setter-它是否具有相同的println
声明,MyValidator上的setter有?是的…在Student Validator中,我得到了引用,但在super类中没有。我得到了解决方案。但我的问题是,它是注入的,证明是setter,那么为什么父类中的依赖项再次变为null父类中的字段从未设置,您已经覆盖了基类中的setter。我t已设置。因此,我正在打印hashcode,请查看我的setter方法。您说过,两个setter中都有相同的println
语句。如果StudentValidator
上的setter覆盖MyValidator
上的setter,并且不调用super
,则MyValidator.studentRep
不能被调用设置。如果设置了它,您将不会得到NullPointerException
,因此您列出的setter不能为您调用validate
方法的实例调用。这两个setter都被调用。这就是为什么我发布了奇怪的行为我有NullPointer的解决方案。但这不是为什么我在依赖dency变量被注入了引用。