Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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
Java Spring 3 autowire始终为空_Java_Spring_Spring Data - Fatal编程技术网

Java Spring 3 autowire始终为空

Java Spring 3 autowire始终为空,java,spring,spring-data,Java,Spring,Spring Data,我很难得到一个非常基本的自动布线。我正试图为我们的系统添加另一个数据源来存储数据,但我遇到了一个问题。我并不是Spring方面的专家,我一直在阅读关于这个问题的文章,似乎我已经正确地设置了所有内容 我尝试将我的组件扫描更改为包括存储库,但这并没有解决问题。我还使用指定的bean名称尝试了@Resource 在我尝试构造jdbcTemplate之前,它不会抛出任何错误,因为数据源为null 我们使用注入的其他类似乎都工作得很好,只是这个使用数据源注入的新类失败了(这是我们第一次进行数据源注入,其他

我很难得到一个非常基本的自动布线。我正试图为我们的系统添加另一个数据源来存储数据,但我遇到了一个问题。我并不是Spring方面的专家,我一直在阅读关于这个问题的文章,似乎我已经正确地设置了所有内容

我尝试将我的组件扫描更改为包括存储库,但这并没有解决问题。我还使用指定的bean名称尝试了@Resource

在我尝试构造jdbcTemplate之前,它不会抛出任何错误,因为数据源为null

我们使用注入的其他类似乎都工作得很好,只是这个使用数据源注入的新类失败了(这是我们第一次进行数据源注入,其他的都是域实体,但我们不会对仓库数据库使用传统的域/实体)

我确信我只是错过了一些非常简单的东西,但到目前为止我还没有找到那是什么

只是一个调试报告

package com.foobar.repositories

@Repository
public class WarehouseRepository
{
    @Autowired
    private DataSource warehouseDataSource; // This is always null

    private JdbcTemplate jdbcTemplate;

    public WarehouseRepository()
    {
        jdbcTemplate = new JdbcTemplate(warehouseDataSource);
    }

    public void debug()
    {
        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet("select * from foobar");
        int i = sqlRowSet.getInt(0);
        String test = sqlRowSet.getString(1);
    }
}
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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
        >
    <context:annotation-config/>
    <context:component-scan base-package="com.foobar.domain"/>
    <context:component-scan base-package="com.foobar.services"/>

    <jpa:repositories base-package="com.foobar.repositories" />

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

    <tx:annotation-driven transaction-manager="transactionManager"/>

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

        <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:env.properties"/>
        </bean>
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
              destroy-method="close">
            <property name="driverClass" value="${env.driver}" />
            <property name="jdbcUrl" value="${env.url}" />
            <property name="user" value="${env.username}" />
            <property name="password" value="${env.password}" />
            <property name="minPoolSize" value="1" />
            <property name="maxPoolSize" value="100" />
            <property name="maxStatements" value="0" />
            <property name="acquireIncrement" value="1" />
        </bean>
        <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="JpaPersistenceUnit" />
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="false"/>
                    <property name="generateDdl" value="false"/>
                    <property name="database" value="MYSQL"/>
                </bean>
            </property>
        </bean>

        <bean id="warehouseDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${env.driver}" />
            <property name="url" value="${env.warehouseUrl}" />
            <property name="username" value="${env.username}" />
            <property name="password" value="${env.password}" />
        </bean>

</beans>

你需要一个公众的接受者和接受者

private DataSource warehouseDataSource;

Spring的自动连接只应用于容器管理的bean,而不应用于直接使用
new
创建的实例,Spring对此一无所知。不要调用
newWarehouseRepository()
,而是在配置文件(XML或Java)中声明bean,或者以编程方式注册bean(这是可能的,但很麻烦)。

自动连接不需要getter和setter。它通过反射直接设置字段。您如何获得对
WarehouseRepository
实例的引用?出于测试目的,我只是手动创建它(new WarehouseRepository()),这是您的问题。如果它不是bean,Spring不会自动连接它。我还尝试删除构造函数,只调用debug()函数,但结果相同。可能重复
package com.foobar.repositories

@Repository
public class WarehouseRepository
{
    @Autowired
    private DataSource warehouseDataSource; // This is always null

    private JdbcTemplate jdbcTemplate;   

    public void debug()
    {
        jdbcTemplate = new JdbcTemplate(warehouseDataSource);// Still blows up here
        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet("select * from foobar");
        int i = sqlRowSet.getInt(0);
        String test = sqlRowSet.getString(1);
    }
}
private DataSource warehouseDataSource;