引用的bean' name'未找到

时间:2015-02-05 12:53:23

标签: java xml spring spring-mvc javabeans

由于一些奇怪的原因,这个dataSource bean可以在我的三个dao-beans中找到,但在其他的dao-bean中找不到(例如Spring-name1)。差异是什么?

  

引用bean' dataSource'找不到

这是代码:

不工作bean(Spring-name1):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="cheopsDAO" class="se.kth.domain.dao.impl.JdbcCheopsDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

工作bean(Spring-name2):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="thresholdDAO" class="se.kth.domain.dao.impl.JdbcThresholdDAO">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

自旋微观-Module.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="database/Spring-Datasource.xml" />
    <import resource="dao-beans/**" />
</beans>

弹簧Datasource.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
        <property value="com.mysql.jdbc.Driver" name="driverClassName" />
        <property value="jdbc:mysql://localhost:3306/test" name="url" />
        <property value="root" name="username" />
        <property value="test" name="password" />
    </bean>
</beans>

我在相应的包中有所需的DAO文件,为什么第二个工作但不是第一个?没有区别......

正手感谢!

1 个答案:

答案 0 :(得分:2)

我通过为我的项目添加配置集(Bean支持)来修复此问题。不知道这是否是正确的解决方案,但它至少删除了错误:

  

右键单击project-&gt; properties-&gt; Spring-&gt; Bean Support-&gt;配置集   (在上一页之前进行扫描),然后我将它们全部添加   一起在随机配置集中。 =)