无法自动连线NamedParameterJdbcTemplate:“ dataSource”或“ jdbcTemplate”为必需项

时间:2019-02-19 11:54:43

标签: spring spring-jdbc

这是WebContent/WEB-INF文件夹中的application-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd ">

    <jpa:repositories base-package="com.java.oxalys.*" />
    <context:component-scan base-package="com.java.oxalys.*" />
    <context:annotation-config></context:annotation-config>

    <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://192.168.1.69;instanceName=ERPX3TOM;databaseName=tomx3v6" />
        <property name="username" value="TESST" />
        <property name="password" value="Oxalys@2019" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.java.oxalys.entity" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate">
       <constructor-arg ref="dataSource"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="myTransactionManager" />
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <!-- persistence -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

制作自动接线的类:

@Repository
public class StockDaoImpl extends JdbcDaoSupport implements StockDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<Stock> getByLocAndItm(String[] locs, String[] itms) {

        List<Stock> ret = new ArrayList<Stock>();

        MapSqlParameterSource parameters = new MapSqlParameterSource();

        parameters.addValue("locs", Arrays.asList(locs));

        String sql = "SELECT s.LOC_0 loc, a.YITMOXA_0 itmoxa FROM [tomx3v6].[TOMEXPL].[STOCK] s join [tomx3v6].[TOMEXPL].[ITMMASTER] a on s.ITMREF_0 = a.ITMREF_0 WHERE s.LOC_0 IN (:locs)";

        List<Map<String, Object>> rows = namedParameterJdbcTemplate.queryForList(sql, parameters);

        for(Map<String, Object> row : rows) {

            Stock tmp = new Stock();
            tmp.setLoc((String) row.get("loc"));
            tmp.setItmoxa((String) row.get("itmoxa"));
            ret.add(tmp);

        }

        return ret;

    }

}

当我与邮递员一起测试时,我会收到错误消息:java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required

那怎么了?

1 个答案:

答案 0 :(得分:0)

您是否真的需要扩展JdbcDaoSupport。因为数据源未自动装配,并且afterPropertiesSet没有检查。

protected void checkDaoConfig() {
    if (this.jdbcTemplate == null) {
        throw new IllegalArgumentException("'dataSource' or 'jdbcTemplate' is required");
  }
}

您还可以看到JdbcDaoSupport在setter / field中没有自动装配,也没有带参数的构造函数。 但是,如果您真的想扩展JdbcDaoSupport,则应在您的类中添加:

@Autowired
private DataSource dataSource;


@PostConstruct
private void initialize() {
    setDataSource(dataSource);
}