Spring 未找到依赖项类型为[…]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件
最近我开始了我与春天的冒险。但奇怪的错误阻止了我。在测试期间,我使用Spring Boot,我的代码如下所示:Spring 未找到依赖项类型为[…]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件,spring,spring-boot,autowired,Spring,Spring Boot,Autowired,最近我开始了我与春天的冒险。但奇怪的错误阻止了我。在测试期间,我使用Spring Boot,我的代码如下所示: package m3.watermeters.srv; …进口 @ComponentScan @EnableAutoConfiguration public class Application { static Logger log = Logger.getLogger(Application.class.getName()); public static voi
package m3.watermeters.srv;
…进口
@ComponentScan
@EnableAutoConfiguration
public class Application {
static Logger log = Logger.getLogger(Application.class.getName());
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring-bean.xml"});
SpringApplication.run(Application.class, args);
}
}
还有两个最重要的类别:
public class AppCfgDAO extends JdbcDaoSupport {
static Logger log = Logger.getLogger(AppCfgDAO.class.getName());
public String getGeneralOwnerName() {
return generalOwnerName;
}
private String generalOwnerName;
public void loadCfg() {
String SQL = "SELECT * FROM utl.cfg_dictionary";
List<CfgItemModel> cfgItemModels = (List<CfgItemModel>) getJdbcTemplate().query(SQL, new CfgRowMapper());
Iterator<CfgItemModel> iterator = cfgItemModels.iterator();
while (iterator.hasNext()) {
CfgItemModel cfgItemModel = iterator.next();
log.info("Cfg value [" + cfgItemModel.getKey() + "=" + cfgItemModel.getValue() + "] valueType=[" + cfgItemModel.getValueType() + "]");
if(cfgItemModel.getKey().equals("general.owner_name")) {
generalOwnerName = cfgItemModel.getValue();
} else {
log.error("Configuration variable [" + cfgItemModel.getKey() + "] defined in config file is not allowed");
}
}
}
}
和spring conf.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder system-properties-mode="OVERRIDE" ignore-resource-not-found="true"
location="classpath:config-default.properties, classpath:config.properties"/>
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="DataSourceBean"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://${db.host}:${db.port}/${db.name}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.password}"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="AppCfgDAOBean" class="m3.watermeters.srv.config.AppCfgDAO">
<property name="dataSource" ref="DataSourceBean" />
</bean>
<bean id="GreetingControlerBean" class="m3.watermeters.srv.GreetingController">
</bean>
</beans>
你知道我的代码哪里有错误吗(发生这种情况可能是因为Spring Boot config类应用程序不知道您的xml配置。请尝试通过添加@ImportResource注释来告诉Spring Boot考虑您的xml配置:
@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:to/xml/config")
public class Application {
//..
}
它是有效的,但我不得不删除:嗨,Michal,是的,因为您已经通过@RestController的注释让Spring意识到了这一点。我建议您应该遵循单一路径,不要混淆xml和注释配置。还有一个明显的趋势是基于注释的配置,因为您将获得很多好处,例如类继承。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greetingController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void m3.watermeters.srv.GreetingController.setAppCfgDAO(m3.watermeters.srv.config.AppCfgDAO); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [m3.watermeters.srv.config.AppCfgDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:to/xml/config")
public class Application {
//..
}