Spring - Autowired无法正常工作

时间:2017-11-16 19:25:12

标签: xml spring configuration autowired

这是我的情况: - /WEB-INF/spring/applicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">  

    <context:annotation-config />

    <bean id="userDAO" class="com.test.repository.user.dao.impl.UserDAOImpl" />

</beans>
  • 的web.xml

    <display-name>Test</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  • 在UserDAOImpl:

    @Log4j2 public class UserDAOImpl implements UserDAO { ... ... }

  • 测试:

    @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration(locations = { "classpath:*applicationContext.xml" })
     public class TestDAO{
    
    @Autowired
    UserDAO userDAO;
    

    ...

    }

  • 的pom.xml:

    ...             org.springframework             春天上下文             $ {} springframework.version         

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
            <scope>test</scope>
        </dependency>
    

    ...

  • 错误:

    没有'com.test.repository.user.dao.impl.UserDAO'类型的限定bean可用:预计至少有1个bean可以作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

1 个答案:

答案 0 :(得分:0)

除非您为UserDAOImpl定义了正确的软件包名称,否则我不会发现任何问题。我刚做了以下设置,它正在运行。

的applicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <context:annotation-config />

    <bean id="userDAO" class="com.mytest.UserDAOImpl" />

</beans> 

UserDAO的

package com.mytest;

public interface UserDAO {
    public void mytest();
}

在UserDAOImpl

package com.mytest;
public class UserDAOImpl implements UserDAO {
    @Override
    public void mytest() {
        System.out.println("In MY test");
    }
}

TestDAO

package com.mytest;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "file:C://<fully qualified path>//applicationContext.xml" })
    public class TestDAO {
        @Autowired
        UserDAO userDAO;

        @Test
        public void testSysout(){
            userDAO.mytest();//prints "In MY test"
        }
    }