Java 无法使用外部jar文件';父项目中的自动连线类
我的项目可以独立使用自动连线选项,我从这个项目创建了一个jar文件。问题是在我将这个jar添加到父项目之后出现的,当我尝试在父项目中使用外部jar文件的类时,我遇到了类似“线程中的异常”main“java.lang.NullPointerException…”的错误 但若我不在我的外部jar文件中使用autowired选项并手动编写代码填充上下文,那个么外部jar文件就可以与我的父项目配合使用 带有自动连线类的外部Jar文件Java 无法使用外部jar文件';父项目中的自动连线类,java,spring,jar,external,autowired,Java,Spring,Jar,External,Autowired,我的项目可以独立使用自动连线选项,我从这个项目创建了一个jar文件。问题是在我将这个jar添加到父项目之后出现的,当我尝试在父项目中使用外部jar文件的类时,我遇到了类似“线程中的异常”main“java.lang.NullPointerException…”的错误 但若我不在我的外部jar文件中使用autowired选项并手动编写代码填充上下文,那个么外部jar文件就可以与我的父项目配合使用 带有自动连线类的外部Jar文件 public class UserService { @Autow
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();
}
}
父项目中的应用程序上下文文件
<!-- 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" />
很简单。您的
UserService
没有连接,因为您使用new
创建了它,并且没有从spring上下文中检索到它
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
缺少其xml配置或@组件,这取决于spring实际连接它的配置。您可能应该创建从外部jar导入的本地spring配置文件。非常简单。您的UserService
没有连接,因为您使用new
创建了它,并且没有从spring上下文中检索到它
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
缺少其xml配置或@组件,这取决于spring实际连接它的配置。您可能应该创建从外部jar导入的本地spring配置文件