我有一个模块,它使用Spring并在单独的jar中使用maven构建 Spring和其他库位于jar文件外部的单独文件夹中 一般来说它看起来像这样:
靶
| --- LIB
| --- updater.jar
问题是,当我使用以下命令运行此jar时
java -jar updater-1.jar com.updater.config.Application
它开始执行但停止时有以下异常:
2015-03-05 09:10:35,966 ERROR [Application.java:40] - Error creating bean with name
'scheduledTask': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.updater.manager.UpdateManager com.updater.config.ScheduledTask.updateManager;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'updateManager': Injection of autowired dependencies
failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private com.updater.persistence.repository.CommonEntityRepository
com.updater.manager.UpdateManager.commonEntityRepository; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'commonEntityRepository': Injection of persistence dependencies failed; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean
of type [javax.persistence.EntityManagerFactory] is defined -
[main] com.updater.config.ScheduledTask
仅当我从生成的jar文件运行应用程序时,才会抛出btw:异常。如果我从IDE运行它(Intellij Idea)应用程序正常工作,没有此异常。
package com.updater.config;
@Configuration
@EnableAutoConfiguration
@EnableScheduling
@ComponentScan(basePackages = "com.updater")
// cannot read external properties by relative path
@PropertySource(name = "external1", value = "file:external.properties",
ignoreResourceNotFound = true)
@Import({ PersistenceConfig.class })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
调度程序:
package com.updater.config;
@Component
public class ScheduledTask {
@Autowired
private Environment env;
@Autowired
private UpdateManager updateManager;
...
}
UpdateManager:
package com.updater.manager;
@Component
public class UpdateManager {
@Autowired
private ChangesService changesService;
@Autowired
private CommonEntityRepository commonEntityRepository;
...
}
CommonEntityRepository:
package com.updater.persistence.repository;
@Component
public class CommonEntityRepository {
@Value(value = "${identity.insert.enable.sql:}")
private String identityInsertEnableSql;
@Value("${identity.insert.disable.sql:}")
private String identityInsertDisableSql;
@PersistenceContext
private EntityManager entityManager;
...
}
的pom.xml
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<mainClass>${start-class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
生成Manifest.MF
Manifest-Version: 1.0
Implementation-Title: updater
Implementation-Version: 1
Archiver-Version: Plexus Archiver
Built-By: Author
Specification-Vendor: SoftwareEntwicklung Beratung Schulung
groupId: com.updater.service
Specification-Title: updater
Implementation-Vendor-Id: com.updater.service
Class-Path:
lib/jsr305-2.0.1.jar
lib/spring-boot-starter-1.1.9.RELEASE.jar
lib/spring-boot-1.1.9.RELEASE.jar
lib/spring-boot-autoconfigure-1.1.9.RELEASE.jar
lib/spring-boot-starter-logging-1.1.9.RELEASE.jar
lib/spring-core-4.0.8.RELEASE.jar
lib/hibernate-entitymanager-4.3.6.Final.jar
lib/spring-data-jpa-1.7.0.RELEASE.jar
lib/spring-data-commons-1.9.0.RELEASE.jar
lib/spring-orm-4.0.7.RELEASE.jar
lib/spring-jdbc-4.0.7.RELEASE.jar
lib/spring-aop-4.0.7.RELEASE.jar
lib/aopalliance-1.0.jar
lib/spring-tx-4.0.7.RELEASE.jar
lib/spring-beans-4.0.7.RELEASE.jar
...
version: 1
Implementation-Vendor: SoftwareEntwicklung Beratung Schulung
Main-Class: com.updater.config.Application
artifactId: updater
Created-By: Apache Maven 3.2.5
Build-Jdk: 1.8.0_31
Specification-Version: 1
已更新
为了更好地理解发生了什么,我没有其他选择,比如调试生成此异常的Spring。
我发现执行app gtom IDE和单独jar之间的初始化存在一些差异。
Spring加载一些工厂
并且urls变量
存在一些差异top是IDE调试 底部是远程jar调试
因此不考虑一些罐子 看起来这是类路径的问题。但至于我的表现是正确的。
此时我不知道接下来要去哪里。