父项目中不能使用外部jar文件的自动装配类

时间:2012-11-25 15:18:20

标签: java spring jar external autowired

我的项目与autowired选项一起工作正常,我从这个项目创建一个jar文件。问题来自我将这个jar添加到父项目后,当我尝试在我的父项目中使用外部jar文件的类时,我得到了错误“线程中的异常”主“java.lang.NullPointerException ...”。

但是如果我不在我的外部jar文件中使用autowired选项并手动编写代码填充上下文。那么外部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)

1 个答案:

答案 0 :(得分:1)

非常简单。您的UserService未连线,因为您使用new创建了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(); } } 并且未从弹簧环境中检索到它。

UserService

或类似的东西。

然而,您的@Component缺少其xml配置或{{1}}以及依赖配置,Spring将实际连接它。您应该创建本地spring配置文件,从外部jar中导入该文件。