0

我会用 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'; [...]
4

1 回答 1

0

我有一个类似的问题。对我来说,答案是在我的 pom.xml 中包含包含“imported-application-context.xml”作为依赖项的项目。

不确定您是否仍在寻找答案,或者这是否也是您问题的答案,但它可能会帮助遇到此问题的其他人。

于 2013-06-20T09:03:15.277 回答