我会用 Maven shade 插件构建一个 Spring - Jnlp 项目,以便只输出一个 jar 文件。该项目有一个依赖项,指的是二级 Spring 项目。生成的 jar 结构正确显示了所有指定的依赖包、类和资源,包括放置在存档根目录中的导入应用程序上下文.xml 。不幸的是,Spring 无法在执行时将配置的 bean 加载到导入的文件中。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- STUFF -->
<dependencies>
<!-- SPRING AND SECONDARY PROJECT DEPENDENCIES -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
应用程序上下文.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true">
<import resource="classpath:imported-application-context.xml"/>
<bean id="myBean" class="myPackage.MyClass">
<property name="myProperty" ref="importedBean" />
</bean>
</beans>
例外
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myBean' defined in the
classpath resource [application-context.xml]:
Cannot resolve reference to bean 'importedBean' while
setting property 'myProperty'; [...]