1

我的项目使用自动装配选项作为独立的工作正常,我从这个项目创建了一个 jar 文件。问题出现在我将此 jar 添加到父项目之后,当我尝试在我的父项目中使用外部 jar 文件的类时,出现类似“异常线程“主”java.lang.NullPointerException...”。

但是,如果我不在我的外部 jar 文件中使用自动装配选项并手动编写代码填充上下文。那么外部 jar 文件适用于我的父项目。

带有自动装配类的外部 Jar 文件

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");

来自外部 jar 文件的 ApplicationContext 文件

<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" />

父项目包文件

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 文件

<!-- 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" />

在我们运行父项目的代码后,我收到以下错误:

 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)
4

1 回答 1

1

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.

于 2012-11-25T16:24:38.397 回答