I am trying to create a separate application (not running on the application server) using Spring, and I am having the following problem:
My standalone application (with the spring turned on) depends on another project (packaged as a jar file) that contains many services in com.application.service (annotated using @Service ).
In the external project, there is no spring-related configuration, and the context of the stand-alone application is very simple, it contains only:
<context:component-scan base-package="com.application" />
Here is an example of a class that depends on a service that cannot be retrieved:
@Service public class StandaloneService { @Autowired private SomeService someService;
StandaloneService contained in a separate application, while SomeService is located in an external bank.
Mistake:
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)}
This is how I create ApplicationContext and try to get 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 do I create a 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 run it (which leads to a crash):
java -jar target/standalone.jar
The strange thing 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
Can someone help me understand why Spring does not see my external services in the first case?
EDIT
This is from the pom.xml external jar file:
<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>