문제

I run here to a very specific Java Spring issue I can not understand. I have created myself an abstract class, f.e:

public abstract class AbstractCrudServiceImpl{

    private GenericDAO baseDao;

    public GenericDAO getBaseDao() {
     return baseDao;
    } 
}

Generic Dao is an interface. I wanted to have the getter (and setter) in the abstract class, so the implementation would be rather simple and clear. [Does not matter if its not atm].

Then have it's implementation, for instance:

public class AgentServiceImpl extends AbstractCrudServiceImpl{

    @Autowired
    private AgentDao baseDao;
}

Using getter of superclass to run all the methods.

My intention was to have a similar effect as if I use XML like:

<bean id="AbstractCrudServiceImpl" class="..." abstract="true" />

<bean id="AgentServiceImpl" parent="AbstractCrudServiceImpl">
    <property name="baseDao" ref="agentDao"/>
</bean>

And to my surprise I got an error saying:

java.lang.NullPointerException
 com.insurance.central.services.impl.AbstractCrudServiceImpl.read(AbstractCrudServiceImpl.java:36)

which basically means, that the property is not set properly, because it's value is still null. Why!?

How should I correct it.

Thanks.

도움이 되었습니까?

해결책

Spring is setting correctly the field baseDao in AgentServiceImpl. The field baseDao in parent class AbstractCrudServiceImpl stays null. In Java you can override a method, not a member variable. Try AgentServiceImpl.baseDao vs. AgentServiceImpl.super.baseDao.

다른 팁

I don't know much about Spring, so I can't speak to the annotations or anything, but your original property is private, so you can't override it. Try changing both to protected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top