我需要从我的maven模块(Java+Spring)及其依赖项创建一个单独的可执行jar。所以我在我的POM中包含了maven-shade-plugin,看起来它打包了所有需要的东西。
当我运行这个jar时,它以NullPointerException失败。看起来它没有找到@Value在我的主类中引用的值,我最终得到了一个NPE。我确实看到applicationContext和properties文件在jar中。这可能是什么原因呢?
applicationContext.xml -
<context:property-placeholder location="classpath:props.properties" ignore-unresolvable="true"/>
Main.java -
@Value("${property.inside.props.file}")
private String propertyName;
pom.xml -
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.sloan.Main.java</Main-Class>
</manifestEntries>
</transformer>
<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>
我试过用汇编插件做同样的事情,但这也带来了NPE。
我发现我错过了什么,加载Spring上下文!现在main。java首先加载上下文-
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF\spring\applicationContext.xml");
重新安装后一切正常
您缺少负责解析此注释的@Bean
将此bean添加到您的配置类
@Bean
public static PropertyPlaceholderConfigurer getValueResolver()
{
return new PropertyPlaceholderConfigurer();
}
的基于XML的教程看这里http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/