gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:12:15 25 4
gpt4 key购买 nike

我有一个批处理项目,正在尝试部署到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com