Reputation: 3591
The below picture is the structure of my project.
And I'm using this method to initializing Spring context.
static ApplicationContext context
= new ClassPathXmlApplicationContext("classpath*:context-*.xml");
When I run main method at Intellij IDEA. Its working fine.
But if I package project to jar by using Maven, And run project by java -jar wireless-service.jar
I got this output.
It looks like that Spring didn't found context-datasource.xml
, context-mybatis.xml
, etc...
Resolved location pattern [classpath*:context-*.xml] to resources []
Loaded 0 bean definitions from location pattern [classpath*:context-*.xml]
So, what is the right config of Spring with Maven?
EDIT
And this picture is structure of wireless-server.jar
EDIT 2
I just change the config path to classpath*:context.xml
and create the context.xml
as
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" default-autowire="byName"
default-lazy-init="false" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="context-datasource.xml"></import>
<import resource="context-mybatis.xml"></import>
<import resource="context-resource.xml"></import>
<import resource="context-service.xml"></import>
</beans>
Then I run wireless-server.jar
again.
I got different error info:
guration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: URL [jar:file:/root/wireless-server-1.0.0-jar-with-dependencies.jar!/context-mybatis.xml]
And Its work well in Intellij IDEA by running main method.
Finally
I modified <build>
section in pom.xml
as this:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<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>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${app.main.class}</Main-Class>
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And now all things just work fine!
Upvotes: 3
Views: 753
Reputation: 5603
From the Spring 3.0 Documentation:
5.7.2.3 Other notes relating to wildcards
Please note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like "classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK's ClassLoader.getResources() method which only returns file system locations for a passed-in empty string (indicating potential roots to search).
Upvotes: 2