java - 无法创建 bean——需要 Autowire 候选项

标签 java spring

我有一个批处理项目,正在尝试部署到 Tomcat 6.0 服务器。我使用 Maven 作为我的构建管理工具,并且项目构建没有错误。但是,一旦我尝试部署,就会收到以下错误:

    SEVERE: Exception sending context initialized event to listener instance of class 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'retrieveKeys': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.obc.idle.dao.PnetDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}      

这是我的 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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
<import resource="applicationContext-sql.xml"/>
<import resource="applicationContext-db.xml" />
<!-- <import resource="applicationContext-mq.xml" /> -->


<context:component-scan base-package="com.company.app.obc.idle" />
<!-- NEEDED FOR JAXB TO WORK -->
<mvc:annotation-driven />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

<bean id="retrieveKeys" class="com.company.obc.idle.batch.RetrieveKeys" />

<bean id="jobProcessUnmarkedPTOEventKeysDetail"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="retrieveKeys" />
    <property name="targetMethod" value="retrieveKeys" />
    <property name="concurrent" value="false" />
</bean>


<bean id="cronTriggerProcessUnmarkedPTOEventKeys" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="jobProcessUnmarkedPTOEventKeysDetail" />
    <!-- TEST -->
    <property name="cronExpression" value="0/15 * * * * ?" />
    <!-- PROD
    <property name="cronExpression" value="0 30 1/6 * * ?" /> -->
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTriggerProcessUnmarkedPTOEventKeys" />
        </list>
    </property>
</bean>

这是我尝试使用 quartz 计时器调用的 RetrieveKeys 类。我尝试了许多不同的方法来使其正常工作,并且这个最新版本尝试命名组件以尝试解决问题:

    @Component(("retrieveKeys"))
    public class RetrieveKeys {

@Resource
PnetDAO PnetDAO;

@Resource
ProcessEvents ProcessEvents;

private static Logger log = Logger.getLogger(RetrieveKeys.class);

    public void retrieveKeys() throws Exception{
        //SOME MORE CODE

我还将包括我的 DAO 及其实现。我想知道为什么问题中会提到这一点,因为还有另一个以相同方式设置的 DAO 类,但错误中没有提到它。

public interface PnetDAO {

public abstract List<UnprocessedKeys> SelectUnprocessedEvents() throws Exception;
public abstract List<IdleEvent> SelectDetailedEventInformation(String uniqkey) throws Exception;
public abstract int UpdatePTOIdle(int statusFlag, String uniqKey) throws Exception;

}

...

@Repository
public class PnetDAOImpl extends NamedParameterJdbcDaoSupport implements PnetDAO {

private static Logger log = Logger.getLogger(PnetDAOImpl.class);

@Resource(name = "queriesBean")
private Properties sql;

@SuppressWarnings("rawtypes")
private Map<Class, RowMapper> rowMappers;

@Autowired
public PnetDAOImpl(@Qualifier("com.company.datasource.PNETPRD") DataSource ds){
    super();
    this.setDataSource(ds);
}

//ACCESSING DATA HERE

我很困惑为什么 Spring 会查看我的 DAO,而我没有在 applicationContext 文件中的任何地方提到它。有人有什么想法吗?谢谢!

最佳答案

您告诉 Spring 在 RetrieveKeys 组件中 Autowiring PNetDAO 实例:

@Resource
PnetDAO PnetDAO;

因此 Spring 尝试查找实现此接口(interface)的 bean,但没有找到,因此抛出异常。

它找不到您的实现类,因为它位于 com.company.obc.idle.dao 包中,并且您告诉 Spring 扫描 com.company 包中的类。 app.obc.idle.

关于java - 无法创建 bean——需要 Autowire 候选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19325642/

相关文章:

java - 读取文件到最后

java - 将 for 循环转换为 java8 风格

Spring Security OAuth2 check_token 端点

java - web.xml 中的属性不起作用 eclipse kepler

java - 使用 Bootstrap 在部署时创建目录

JavaFX2 WebView 和内存中图像

java - 关于为什么按下按键时三角形不旋转有什么建议吗?

java - 使用 MySQL 的 DataJpaTest 存储库测试,Hibernate 不会将用户设置为在 H2 DB 中自动递增

Spring 批处理 : How to setup a FlatFileItemReader to read a json file?

java - Spring在非常简单的程序中忽略@Qualifier