maven - Autowiring classes from external jar with Spring -
i'm trying build standalone application (not running inside application server) spring , i'm facing following problem :
my standalone application (spring enabled) depending on project (bundled jar) contains lot of services in com.application.service
(annotated @service
).
there no spring related configuration in external project , standalone application context simple, contains :
<context:component-scan base-package="com.application" />
here example of class depends on service can't acquired :
@service public class standaloneservice { @autowired private someservice someservice; // ... }
standaloneservice
contained in standalone application while someservice
in external jar.
the error :
caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.application.someservice] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
here how i'm creating applicationcontext
, trying acquire 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 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 (which leads failure) :
java -jar target/standalone.jar
what strange if run way 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
does me figure out why spring can't see external services in first case ?
edit
this pom.xml of 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>
three months later, have response : annotation scan not scanning external jars in classpath
as stated in accepted answer, when using -jar option -cp option ignored.
running application way made working expected !
java -cp target/lib/external.jar:target/standalone.jar package.main
Comments
Post a Comment