问题描述
我有一个spring应用程序,它正在拉入一个公共用的罐子.此jar具有带注释的DTO类.运行mvn clean build命令成功运行并生成jar.一旦我运行java -jar target/MyApp-1.0.0.BUILD-SNAPSHOT.jar,我将得到以下错误.
I have a spring application that is pulling in a commons jar. This jar has DTO classes with annotations. Running the mvn clean build command runs successfully and builds the jar. Once i run java -jar target/MyApp-1.0.0.BUILD-SNAPSHOT.jar i will get the below error.
我打开了MyApp-1.0.0.BUILD-SNAPSHOT.jar,并在该jar中找到了我所有的类.
I have opend the MyApp-1.0.0.BUILD-SNAPSHOT.jar and found all my classes inside that jar.
我已打开快照中包含MyApplicationJobDTO的jar,并确认文件在那里.
I have opened the jar that contains MyApplicationJobDTO inside my snapshot and verified the file is there.
似乎在运行时Java即使找到了该类也找不到该类.我不认为我应该向Java类路径添加任何内容,因为该jar位于快照jar内.我陷入困境,不确定为什么会出现运行时错误.
It seems that at runtime java cannot find the class even though the class is there. I dont believe i should have to add anything to java class path since the jar is located inside of the snapshot jar. I am stuck and unsure of why i am getting runtime errors.
Caused by: java.lang.IllegalArgumentException: Not an managed type: class com...MyApplicationJobDTO
带有注释的类
@Component
@Entity
public class MyApplicationJobDTO implements Serializable {
我要运行的命令
mvn clean package && java -jar target/MyApp-1.0.0.BUILD-SNAPSHOT.jar
我的主班
@EnableJpaRepositories
@ComponentScan
@EntityScan
@Configuration
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("file:src/main/java/com/myapp/beans.xml");
SpringApplication.run(Application.class, args);
}
}
Beans.xml
Beans.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyApplicationJobController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.myapp.xx.yy.models.MyApplicationJobDAO com.myapp.xx.yy.web.MyApplicationJobController.MyApplicationJobDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyApplicationJobDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.myapp.aa.bb.myapptest.commons.dto.MyApplicationJobDTO
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at com.myapp.xx.yy.Application.main(Application.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.myapp.xx.yy.models.MyApplicationJobDAO com.myapp.xx.yy.web.MyApplicationJobController.MyApplicationJobDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyApplicationJobDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.myapp.aa.bb.myapptest.commons.dto.MyApplicationJobDTO
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
... 22 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyApplicationJobDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.myapp.aa.bb.myapptest.commons.dto.MyApplicationJobDTO
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1554)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:964)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)
... 24 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.myapp.aa.bb.myapptest.commons.dto.MyApplicationJobDTO
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:88)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:68)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
编辑如果我在我的spring项目中手动添加了需要在我的外部jar中使用的所有类,那么它将起作用.我仍然无法从外部jar中加载类
edit If i manually add all the classes in my external jar that need to be used in my spring project it works. I still cannot get the classes to load from an external jar
推荐答案
删除 @ EntityScan ,它已已弃用.如果使用@EntityScan,它将覆盖@ComponentScan
Remove @EntityScan it is Deprecated. If you use the @EntityScan it will override @ComponentScan
或
您应指定基本软件包以扫描JPA实体. @EntityScan("com.myapp.xx.yy.models")
you should specify the base package to scan for the JPA entities.@EntityScan("com.myapp.xx.yy.models")
这篇关于带有外部jar'非托管类型'的Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!