views:

80

answers:

3

In dependency A I have the following:

<beans>
    <bean
        id="simplePersonBase"
        class="com.paml.test.SimplePerson"
        abstract="true">
        <property
            name="firstName"
            value="Joe" />
        <property
            name="lastName"
            value="Smith" />
    </bean>
</beans>

And then in project B, I add A as a dependency and have the following config:

<?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"&gt;
    <bean
        id="simplePersonAddress01"
        parent="simplePersonBase">
        <property
            name="firstName"
            value="BillyBob" />
        <property
            name="address"
            value="1060 W. Addison St" />
        <property
            name="city"
            value="Chicago" />
        <property
            name="state"
            value="IL" />
        <property
            name="zip"
            value="60613" />
    </bean>
</beans>

When I use ClassPathXmlApplicationContext like so:

    BeanFactory beanFactory = new ClassPathXmlApplicationContext( new String[] {
        "./*.xml" } );

    SimplePerson person = (SimplePerson)beanFactory.getBean( "simplePersonAddress01" );
    System.out.println( person.getFirstName() );

Spring complains as it can not resolve the parent xml.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'simplePersonBase' is defined

I am sure there is a way to do this, however, I have not found it. Does anyone know how?

A: 

Does A.jave have the corresponding xml file? In project A, did you the xml inside src/main/resources?

binil
Yes, the first xml snippet is in src/main/resources of project A.
javamonkey79
+2  A: 

Try with the classpath*: prefix.

lexicore
I saw this before but another error was misleading me. For the record, this is what I have when changed: BeanFactory beanFactory = new ClassPathXmlApplicationContext( new String[] { "classpath*:*.xml" } );
javamonkey79
A: 

Not really an answer to the question - but beware of this approach.

It can be a nightmare resolve errors. Imagine getting an spring error on startup - the only way to resolve it is cracking open all the jars to find any application contexts held within.

At the very least put the application context files in a distinct package and specify any you wish to use by name.

A *.xml from the root of the classpath is a recipe for disaster.

Pablojim