odincer
odincer

Reputation: 329

Can not use external jar file's autowired class in parent project

My project works fine with autowired options as standalone and i create a jar file from this project.Problem comes after i added this jar to parent project, when i try to use external jar file's class in my parent project i got error like "Exception in thread "main" java.lang.NullPointerException...".

But if i don't use autowired option in my external jar file and write code fill context manually.Then external jar file works fine with my parent project.

External Jar File With Autowired Class

public class UserService {


@Autowired 
UserRepository userRepository;

@Autowired
Movie2Repository movieRepository;

@Autowired
PersonRepository personRepository;


ConfigurableApplicationContext context;
public UserService(){
    // context = new ClassPathXmlApplicationContext("META-INF/spring/application-context.xml");
}


public void closeApp(){

    //context.close();
}

public void  insert(){
     //userRepository = context.getBean(UserRepository.class);
     System.out.println("user vvvv");
     User u = new User();
     u.firstname="dede";
     userRepository.save(u);
     System.out.println("user eklendi");

ApplicationContext file from external jar file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
    http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- JPA -->

<context:property-placeholder
    location="/META-INF/spring/database.properties" />

<import resource="/database.xml"/>


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

 <bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false" />
    <property name="database" value="MYSQL" />
</bean>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>

<jpa:repositories base-package="org.springframework.data.example.jpa" />

Parent Project Package File

package abcdef;
import org.springframework.data.example.*; 
import org.springframework.data.example.jpa.UserService;
import org.springframework.data.example.jpa.UserService2;

public class abcde {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            UserService u= new UserService();
            u.insert();

        }


}

ApplicationContext file from parent project

<!-- JPA -->

<context:property-placeholder
    location="/META-INF/spring/database.properties" />

<import resource="/database.xml"/>

<import resource="classpath*:/META-INF/spring/*.xml" />


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

 <bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false" />
    <property name="database" value="MYSQL" />
</bean>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>

<jpa:repositories base-package="org.springframework.data.example.jpa" />

After we run parent project's code i got the below error:

 user vvvv
Exception in thread "main" java.lang.NullPointerException
    at org.springframework.data.example.jpa.UserService.insert(UserService.java:47)
    at abcdef.abcde.main(abcde.java:13)

Upvotes: 1

Views: 2855

Answers (1)

Udo Held
Udo Held

Reputation: 12548

Pretty simple. Your UserService isn't wired because you created it with new and didn't retrieved it from the spring context.

public class abcde {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/spring/application-context.xml");
        UserService u = ctx.getBean("userService");
        u.insert();

    }
}

or something like that.

However your UserService is lacking its xml configuration or the @Component and depending configuration that spring would actually wire it. You should probably create your local spring configuration file that imports the one from the external jar.

Upvotes: 1

Related Questions