Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Repository from other Jar is null. How to initialize it?

I have a project that does data access in one project and to use it in another project as spring jar. I created jar using gradle maven-publish plugin. I initialized this Jar in my other project test case like this

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

Class where Repository is being used. I used like this

public class TenantProvisioningManager {

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

    @Autowired
    TProductRepository tProductRepository;
}

Repository is null here. How to initialize repository from other jar in this seperate project?

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.xml and TProductRepository are in other project while TenantProvisioningManager is in seperate project.

like image 816
Bilal Shah Avatar asked Feb 24 '16 19:02

Bilal Shah


1 Answers

Make sure your TenantProvisioningManager is also managed by spring. Annotate is as @Component

like image 188
WeMakeSoftware Avatar answered Oct 20 '22 15:10

WeMakeSoftware