Spring - 来自其他Jar的存储库为null。如何初始化?

时间:2016-02-24 19:50:17

标签: spring gradle spring-data

我有一个项目在一个项目中进行数据访问,并在另一个项目中用作spring jar。我使用gradle maven-publish插件创建了jar。我在我的其他项目测试用例中初始化了这个Jar

@ContextConfiguration(locations = {"classpath*:spring-config.xml"})
@Transactional
public class TenantProvisioningManagerTest extends AbstractTestNGSpringContextTests {}

正在使用Repository的类。我喜欢这个

public class TenantProvisioningManager {

    private static final Logger logger = LogManager.getLogger(TenantProvisioningManager.class);

    @Autowired
    TProductRepository tProductRepository;
}

存储库在此null。如何在这个单独的项目中从其他jar初始化存储库?

spring-config.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:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.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
              ">
<context:property-placeholder
        location="file://#{systemEnvironment['GRADLE_USER_HOME']}/gradle.properties"/>
<!-- the base package for spring data jpa repository interfaces -->
<jpa:repositories base-package="com.asklytics.dao.repos" />

<!-- Enable the component scan (auto wiring etc) for the following package -->
<context:component-scan base-package="com.asklytics" />

<!-- Make sure the following is specified to enable transaction  -->
<tx:annotation-driven />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!--  This defines the entity manager factory with some custom properties -->
<bean id='entityManagerFactory' class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
    <property name="persistenceUnitName" value="persitanceUnit"/>
    <property name='dataSource' ref='dataSource' />
</bean>

<bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'>
    <property name='driverClassName' value='com.mysql.jdbc.Driver' />
    <property name='url' value="${awsCamelDbUrl}" />
    <property name='username' value="${awsCamelDbUsername}" />
    <property name='password' value="${awsCamelDbPassword}" />
</bean>

spring-config.xmlTProductRepository在其他项目中,TenantProvisioningManager处于单独项目中。

1 个答案:

答案 0 :(得分:2)

确保您的TenantProvisioningManager也由春季管理。注释为@Component