Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading spring application context files that are inside a jar in classpath

I am trying to use ClassPathXmlApplicationContext in my java standalone code to load applicationContext.xml that is inside a jar file which is in my class path.

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/applicationContext*.xml"); 

applicationContext.xml entry as follows,

 <bean id="myAdder" class="com.foo.bar.MyAdder">         <property name="floatAdder" ref="floatAdder"/>             </bean> 

And, when I try to load a bean that way I am getting NoSuchBeanException. Can't a bean by loaded in this way?

The jar file is added to my classpath as a maven dependency. When I see the Java Build Path in Eclipse for this project, I see this jar linked as M2_REPO/.../..

I was assuming I can load the bean inside the jar file as the jar is in classpath this way. Am I missing something?

Thanks,Abi

like image 816
Abhishek Avatar asked Jun 10 '11 07:06

Abhishek


People also ask

What is classpath in Spring application context?

The Spring container is responsible for instantiating and managing the lifecycle of Spring beans. The ClassPathXmlApplicationContext is a class that implements the org. springframework. context. ApplicationContext interface.

How is ApplicationContext XML loaded?

Show activity on this post. Then you can use the WebApplicationContext to load the context: WebApplicationContext context = WebApplicationContextUtils. getRequiredWebApplicationContext(servlet.

Where is the classpath in Spring boot?

It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.

How application context works internally in Spring?

context. ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.


2 Answers

This should work, I just created the 2 projects and checked.

Project A (standard Maven project created with STS) has applicationContext.xml in src/main/resources.

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.D</groupId> <artifactId>A</artifactId> <version>0.0.1-SNAPSHOT</version>  <properties>             <spring.version>3.0.5.RELEASE</spring.version> </properties>  <dependencies>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-context</artifactId>         <version>${spring.version}</version>     </dependency> </dependencies> </project> 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  <bean id="myAdder" class="com.foo.bar.MyAdder">     <property name="foo" value="bar" /> </bean>  </beans> 

Project B:

pom.xml: same as A, except A is added as dependency:

<dependency>    <groupId>org.D</groupId>    <artifactId>A</artifactId>    <version>0.0.1-SNAPSHOT</version> </dependency> 

Start.java in project B:

public static void main(String[] args) {     ApplicationContext context = new ClassPathXmlApplicationContext(             "classpath*:**/applicationContext*.xml");      MyAdder myAdder = (MyAdder) context.getBean("myAdder");     System.out.println(myAdder.getFoo()); } 

mvn install A first, then run Start in project B.

like image 200
abalogh Avatar answered Oct 05 '22 10:10

abalogh


Do you really need the classpath*: prefix on that location? (Is that * legal?) I would have expected something more like:

ApplicationContext context = new ClassPathXmlApplicationContext("**/applicationContext*.xml); 
like image 45
Darien Avatar answered Oct 05 '22 10:10

Darien