使用Spring从外部jar自动装配类 [英] Autowiring classes from external jar with Spring
问题描述
我正在尝试使用Spring构建一个独立的应用程序(不在应用程序服务器内部运行),并且遇到了以下问题:
I'm trying to build a standalone application (not running inside an application server) with Spring and I'm facing the following problem :
我的独立应用程序(启用了spring)依赖于另一个项目(打包为jar),该项目在com.application.service
(用@Service
注释)中包含很多服务.
My standalone application (spring enabled) is depending on another project (bundled as a jar) which contains a lot of services in com.application.service
(Annotated with @Service
).
外部项目中没有与spring相关的配置,并且独立的应用程序上下文非常简单,仅包含:
There is no spring related configuration in the external project and the standalone application context is very simple, it only contains :
<context:component-scan base-package="com.application" />
以下是Class的示例,该类依赖于无法获取的服务:
Here is an example of Class that depends on a service which can't be acquired :
@Service
public class StandaloneService {
@Autowired
private SomeService someService;
// ...
}
StandaloneService
包含在独立应用程序中,而SomeService
包含在外部jar中.
StandaloneService
is contained in the standalone application while SomeService
is in the external jar.
错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.application.SomeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.application.SomeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这是我创建ApplicationContext
并尝试获取我的服务的方式:
Here is how I'm creating the ApplicationContext
and trying to acquire my service :
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
BeanFactory factory = (BeanFactory) context;
StandaloneService standalone = factory.getBean(StandaloneService.class);
}
我如何构建独立应用程序:
How I'm building the standalone application :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<classpathPrefix>./lib/</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>com.application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
我如何运行它(导致失败):
How I'm running it (which leads to the failure) :
java -jar target/standalone.jar
奇怪的是,如果我以这种方式运行它,它将起作用:
What is strange is that if I run it this way it works :
mvn "-Dexec.args=-classpath %classpath com.application.Main" -Dexec.executable=/usr/lib/jvm/java-7-openjdk/bin/java -Dexec.classpathScope=runtime process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
有人可以帮助我弄清楚为什么Spring在第一种情况下看不到我的外部服务吗?
Could anyone help me figure out why Spring can't see my external services in the first case ?
编辑
这是来自外部jar的pom.xml:
This is from the pom.xml of the external jar :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
推荐答案
三个月后,由于以下原因,我现在有了响应: 注释扫描不扫描类路径中的外部jars
Three months later, I now have the response thanks to this : Annotation scan not scanning external jars in classpath
如已接受的答案所述,使用 -jar 选项时,将忽略 -cp 选项.
As stated in the accepted answer, when using the -jar option the -cp option is ignored.
以这种方式运行我的应用程序可以使其按预期运行!
Running my application this way made it working as expected !
java -cp target/lib/external.jar:target/standalone.jar package.Main
这篇关于使用Spring从外部jar自动装配类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!