Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowiring classes from external jar with Spring

I'm trying to build a standalone application (not running inside an application server) with Spring and I'm facing the following problem :

My standalone application (spring enabled) is depending on another project (bundled as a jar) which contains a lot of services in com.application.service (Annotated with @Service).

There is no spring related configuration in the external project and the standalone application context is very simple, it only contains :

<context:component-scan base-package="com.application" />

Here is an example of Class that depends on a service which can't be acquired :

@Service
public class StandaloneService {

    @Autowired
    private SomeService someService;

    // ...
}

StandaloneService is contained in the standalone application while SomeService is in the external jar.

The error :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.application.SomeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Here is how I'm creating the ApplicationContext and trying to acquire my 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 the 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 it (which leads to the failure) :

java -jar target/standalone.jar

What is strange is that if I run it this way it 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

Could anyone help me figure out why Spring can't see my external services in the first case ?

EDIT

This is from the pom.xml of the 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>
like image 210
Alexandre Jacob Avatar asked Jul 17 '13 16:07

Alexandre Jacob


1 Answers

Three months later, I now have the response thanks to this : Annotation scan not scanning external jars in classpath

As stated in the accepted answer, when using the -jar option the -cp option is ignored.

Running my application this way made it working as expected !

java -cp target/lib/external.jar:target/standalone.jar package.Main
like image 157
Alexandre Jacob Avatar answered Nov 15 '22 03:11

Alexandre Jacob