进入Spring几周后,我遇到了 NoSuchBeanDefinitionException 。试图在这里找到解决其他问题的解决方案,但没有运气。 尝试在Eclipse中启动我的简单Spring应用程序时出现此错误:
aug 29, 2015 7:33:45 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void by.blabla.spring.test.OffersDAO.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我使用@Autowired注释在OffersDAO.java中只有一个依赖注入。实现DataSource接口的BasicDataSource bean在beans.xml配置文件中定义,但我的应用程序无法以某种方式连接到它。
配置文件 beans.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="by.blabla.spring.test"></context:component-scan>
<context:property-placeholder location="by/blabla/spring/props/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
</beans>
主要应用程序文件 App.java :
package by.blabla.spring.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("by/blabla/spring/test/beans/beans.xml");
OffersDAO offersDao = (OffersDAO)context.getBean("offersDao");
List<Offer> list = offersDao.getOffers();
for(Offer offer : list){
System.out.println(offer);
}
((ClassPathXmlApplicationContext)context).close();
}
}
OffersDAO.java :
package by.blabla.spring.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
@Component("offersDao")
public class OffersDAO {
private JdbcTemplate jdbc;
@Autowired
public void setDataSource(DataSource jdbc) {
this.jdbc = new JdbcTemplate(jdbc);
}
public List<Offer> getOffers() {
return jdbc.query("select * from offers", new RowMapper<Offer>() {
public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {
Offer offer = new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setEmail(rs.getString("email"));
offer.setText(rs.getString("text"));
return offer;
}
});
}
}
答案 0 :(得分:0)
在你的dao类中,ter不是使用DataSource类型声明的属性。 IOC尝试列出要使用Reflection注入的属性,但它找不到bean配置文件中提到的名称和类型的参数
您的代码应如下
@Component("offersDao")
public class OffersDAO {
private JdbcTemplate jdbc;
private DataSource datasource;
@Autowired public void setDataSource(DataSource ds) {
this.datasource=ds;
this.jdbc = new JdbcTemplate(ds);
}
建议: 最好在spring上下文中创建ur jdbc模板并注入它而不是注入数据源并创建模板对象......
答案 1 :(得分:0)
试试这个:
@Component("offersDao")
public class OffersDao implements InitializingBean {
private JdbcTemplate jdbc;
@Autowired
private DataSource dataSource;
@Override
public void afterPropertiesSet() throws Exception {
this.jdbc = new JdbcTemplate(this.dataSource);
}
public List<Offer> getOffers() {
return jdbc.query("select * from offers", new RowMapper<Offer>() {
public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {
Offer offer = new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setEmail(rs.getString("email"));
offer.setText(rs.getString("text"));
return offer;
}
});
}
}