1

我有一个尝试部署到 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>

这是我尝试使用石英计时器调用的 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

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

4

1 回答 1

2

您告诉 Spring 在 RetrieveKeys 组件中自动装配 PNetDAO 实例:

@Resource
PnetDAO PnetDAO;

所以Spring试图找到一个实现这个接口的bean,没有找到,因此抛出异常。

它找不到您的实现类,因为它在 packagecom.company.obc.idle.dao中,并且您告诉 Spring 从 package 中扫描类com.company.app.obc.idle

于 2013-10-11T19:25:04.437 回答