Note that there are some explanatory texts on larger screens.

plurals
  1. POAutowiring classes from external jar with Spring
    primarykey
    data
    text
    <p>I'm trying to build a standalone application (not running inside an application server) with Spring and I'm facing the following problem :</p> <p>My standalone application (spring enabled) is depending on another project (bundled as a jar) which contains a lot of services in <code>com.application.service</code> (Annotated with <code>@Service</code>).</p> <p>There is no spring related configuration in the external project and the standalone application context is very simple, it only contains :</p> <p><code>&lt;context:component-scan base-package="com.application" /&gt;</code></p> <p>Here is an example of Class that depends on a service which can't be acquired :</p> <pre><code>@Service public class StandaloneService { @Autowired private SomeService someService; // ... } </code></pre> <p><code>StandaloneService</code> is contained in the standalone application while <code>SomeService</code> is in the external jar.</p> <p>The error :</p> <p><code>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)} </code></p> <p>Here is how I'm creating the <code>ApplicationContext</code> and trying to acquire my service :</p> <pre><code>public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); BeanFactory factory = (BeanFactory) context; StandaloneService standalone = factory.getBean(StandaloneService.class); } </code></pre> <p>How I'm building the standalone application :</p> <pre><code>&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt; &lt;version&gt;2.4&lt;/version&gt; &lt;configuration&gt; &lt;archive&gt; &lt;index&gt;true&lt;/index&gt; &lt;manifest&gt; &lt;classpathPrefix&gt;./lib/&lt;/classpathPrefix&gt; &lt;addClasspath&gt;true&lt;/addClasspath&gt; &lt;mainClass&gt;com.application.Main&lt;/mainClass&gt; &lt;/manifest&gt; &lt;/archive&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;copy-dependencies&lt;/id&gt; &lt;phase&gt;package&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;copy-dependencies&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;outputDirectory&gt;${project.build.directory}/lib&lt;/outputDirectory&gt; &lt;overWriteReleases&gt;false&lt;/overWriteReleases&gt; &lt;overWriteSnapshots&gt;false&lt;/overWriteSnapshots&gt; &lt;overWriteIfNewer&gt;true&lt;/overWriteIfNewer&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>How I'm running it (which leads to the failure) :</p> <p><code>java -jar target/standalone.jar</code></p> <p>What is strange is that if I run it this way it works :</p> <p><code>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</code></p> <p>Does anyone could help me figure out why Spring can't see my external services in the first case ?</p> <p><strong>EDIT</strong></p> <p>This is from the pom.xml of the external jar :</p> <pre><code>&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt; &lt;version&gt;2.4&lt;/version&gt; &lt;configuration&gt; &lt;archive&gt; &lt;manifest&gt; &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt; &lt;addClasspath&gt;true&lt;/addClasspath&gt; &lt;/manifest&gt; &lt;/archive&gt; &lt;/configuration&gt; &lt;/plugin&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload