简体   繁体   中英

DAO null in spring bean class with xml config

I'm trying to inject the uploadDao in the bean DemoHandler (XML below). But as expected it is Null. The question is how do I inject the DAO correctly.

Right now, there is a method call from WebUploadServiceImpl to DemoHandler and I can add 1 more parameter and send the value I need from uploadDao, but then that method is reused for multiple java classes and all those methods need to be refactored and that new param will not be relevant to the other classes.

Is there a way to get uploadDao to inject in DemoHandler? Any help is much appreciated. Thanks

Spring XML:

<bean id="wuService" class="com.abcd.service.WebUploadServiceImpl">
        <property name="uploadDao" ref="uploadDao" />
        <property name="webHandlers">
            <set>
                <bean class="com.abcd.service.handler.DemoHandler" />
            </set>
        </property>
</bean>

Code:

public class DemoHandler {

private UploadDao uploadDao;

   public List handleM(...) {
        ...
        Period period = uploadDao.anyMethod(..); --- Null here
   }    
}

If DemoHandler needs to set a dao, why don't you do something like this ...

<bean id="uploadDao" class="whatever.dao.UploadDaoImpl"/>

<bean id="wuService" class="com.abcd.service.WebUploadServiceImpl">
        <property name="uploadDao" ref="uploadDao" />
        <property name="webHandlers">
            <set>
                <bean class="com.abcd.service.handler.DemoHandler">
                    <property name="uploadDao" ref="uploadDao" />
                </bean>
            </set>
        </property>
</bean>

You just need to replicate the injection mechanism already used in WebUploadServiceImpl . You haven't shown that code, I don´t know if you use @Autowired , a set method or any other possibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM