Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(JavaSpring)NoSuchBeanDefinitionException:未找到[javax.sql.DataSource]类型的合格bean_Java_Eclipse_Spring - Fatal编程技术网

(JavaSpring)NoSuchBeanDefinitionException:未找到[javax.sql.DataSource]类型的合格bean

(JavaSpring)NoSuchBeanDefinitionException:未找到[javax.sql.DataSource]类型的合格bean,java,eclipse,spring,Java,Eclipse,Spring,春天刚过几个星期,我就没有这样的定义例外了。试图找到一个解决方案阅读其他问题在这里,所以没有运气。 在Eclipse中尝试启动我的简单Spring应用程序时出现以下错误: aug 29, 2015 7:33:45 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh WARNING: Exception encountered during context initialization - ca

春天刚过几个星期,我就没有这样的定义例外了。试图找到一个解决方案阅读其他问题在这里,所以没有运气。 在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: {}
我在OffersDAO.java中只有一个使用@Autowired注释的依赖项注入。实现DataSource接口的BasicDatasourcebean是在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>
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();
    }
}
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;
            }
        });
    }
}

主应用程序文件App.java:

<?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>
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();
    }
}
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;
            }
        });
    }
}
package by.blabla.spring.test;
导入java.util.List;
导入org.springframework.context.ApplicationContext;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
公共类应用程序{
公共静态void main(字符串[]args){
ApplicationContext context=new ClassPathXmlApplicationContext(“by/blabla/spring/test/beans/beans.xml”);
OffersDAO OffersDAO=(OffersDAO)context.getBean(“OffersDAO”);
List List=offersDao.getOffers();
用于(报价:列表){
系统输出打印项次(报价);
}
((ClassPathXmlApplicationContext)上下文).close();
}
}
OffersDAO.java

<?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>
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();
    }
}
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;
            }
        });
    }
}
package by.blabla.spring.test;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.List;
导入javax.sql.DataSource;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.jdbc.core.RowMapper;
导入org.springframework.stereotype.Component;
@组件(“offersDao”)
公开课{
私有jdbc模板jdbc;
@自动连线
public void setDataSource(数据源jdbc){
this.jdbc=新的jdbc模板(jdbc);
}
公共列表getOffers(){
返回jdbc.query(“select*from offers”,new RowMapper(){
public Offer mapRow(ResultSet rs,int rowNum)抛出SQLException{
报价=新报价();
offer.setId(rs.getInt(“id”);
offer.setName(rs.getString(“name”);
offer.setEmail(rs.getString(“email”);
offer.setText(rs.getString(“text”);
还盘;
}
});
}
}

在dao类中,ter不是使用类型DataSource声明的属性。IOC试图列出要使用反射注入的属性,但未找到具有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模板并将其注入,而不是注入数据源并创建模板对象…

尝试以下方法:

@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;
        }
    });
}
}
@组件(“offersDao”)
公共类OffersDao实现了初始化bean{
私有jdbc模板jdbc;
@自动连线
私有数据源;
@凌驾
public void afterPropertieSet()引发异常{
this.jdbc=新的jdbc模板(this.dataSource);
}
公共列表getOffers(){
返回jdbc.query(“select*from offers”,new RowMapper(){
public Offer mapRow(ResultSet rs,int rowNum)抛出SQLException{
报价=新报价();
offer.setId(rs.getInt(“id”);
offer.setName(rs.getString(“name”);
offer.setEmail(rs.getString(“email”);
offer.setText(rs.getString(“text”);
还盘;
}
});
}
}

尝试了您的建议,但仍收到异常
未找到符合依赖项要求的[javax.sql.DataSource]类型的bean:应至少有1个bean符合此依赖项的autowire候选项要求。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)
对您的解决方案也没有什么好处:(
创建名为“offersDao”的bean时出错:自动关联依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联方法:public void by.blablablabla.spring.test.offersDao.setDataSource(javax.sql.DataSource);嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到符合依赖项要求的[javax.sql.DataSource]类型的bean:应至少有1个bean符合此依赖项的autowire候选项要求。依赖项批注:{}
我猜是类型不匹配…在配置文件中,u已给出“org.apache.commons.dbcp.BasicDataSource”但在setter方法“javax.sql.DataSource”中。请确保在这两个位置使用相同的类型…尝试使用@Resource(name=“DataSource”)显式指向bean失败。u plz是否可以提供完整的堆栈跟踪(整个日志,从开始到结束)
Spring的版本
?Spring的版本是4.1.7。请在此处查找日志(&C):