30

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

Abhishek
  • 6,862
  • 22
  • 62
  • 79
  • Can you post your code where you try instantiating the appContext? – abalogh Jun 10 '11 at 07:31
  • @abalogh: I have edited the post with the line of code I am using – Abhishek Jun 10 '11 at 07:33
  • This seems ok, also the exception you are getting seems to indicate that the problem is not with creating the appContext, but getting the bean from it. How are you getting the bean, and please also post the definition of it in the applicationContext.xml – abalogh Jun 10 '11 at 07:48
  • @abalogh: Added it..that actually makes me wonder if Eclipse "Java Build path" is same as CLASSPATH :-( – Abhishek Jun 10 '11 at 07:53
  • @abalogh: I added the same way of bean loading in a test class in the project that has been built as the JAR. It works as charm. ie. for example A.jar is set in Java Build Path of Project B as Maven dependency -> The bean loading in code inside Project B from applicationContext.xml in A.jar does not work. I try to load the bean in same project A works fine. I believe its not an issue with the mapping nor code its something with the configuration..if I am not wrong.. – Abhishek Jun 10 '11 at 07:58
  • Hm you mean you have an applicationContext.xml in A.jar and one in B.jar as well? – abalogh Jun 10 '11 at 08:00
  • @abalogh: Application context in a.jar is being loaded in a project B. A.jar is added as maven dependency in project B. – Abhishek Jun 10 '11 at 08:02

2 Answers2

37

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.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
abalogh
  • 8,239
  • 2
  • 34
  • 49
  • Thanks a bunch. I was unable to figure out what the issue was. I just cleared up everything dumped the projects and recreated from scratch and it works. You response clarified my question, I have voted up for this. – Abhishek Jun 10 '11 at 09:25
1

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);
Darien
  • 3,482
  • 19
  • 35
  • Tried it, does not work as well. I am getting the same NoSuchBeanDefinitionException – Abhishek Jun 10 '11 at 08:10
  • 3
    Had a similar problem and came across this post. The explanation for the `classpath*:` can be found here: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/resources.html#resources-classpath-wildcards – Luke Apr 08 '13 at 17:44
  • sooooo many thanks, I wrote an modular application and just it works for me – Seyed Ali Roshan Mar 01 '17 at 12:23