简体   繁体   English

使用xml配置的Spring bean类中的DAO空

[英]DAO null in spring bean class with xml config

I'm trying to inject the uploadDao in the bean DemoHandler (XML below). 我试图将uploadDao注入bean DemoHandler(下面的XML)中。 But as expected it is Null. 但正如预期的那样,它为Null。 The question is how do I inject the DAO correctly. 问题是如何正确注入DAO。

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. 现在,有一个从WebUploadServiceImpl到DemoHandler的方法调用,我可以再添加1个参数并从uploadDao发送所需的值,但是该方法可用于多个Java类,并且所有这些方法都需要重构,并且需要新的参数与其他类别无关。

Is there a way to get uploadDao to inject in DemoHandler? 有没有办法让uploadDao注入DemoHandler? Any help is much appreciated. 任何帮助深表感谢。 Thanks 谢谢

Spring XML: 春季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 ... 如果DemoHandler需要设置dao,为什么不这样做呢?

<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 . 您只需要复制WebUploadServiceImpl已经使用的注入机制。 You haven't shown that code, I don´t know if you use @Autowired , a set method or any other possibility. 您尚未显示该代码,我不知道您是否使用@ @Autowired ,set方法或任何其他可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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