开发者

How to use a Spring config file in a Maven dependency

开发者 https://www.devze.com 2022-12-26 19:01 出处:网络
In dependency A I have the following: <beans> <bean id=\"simplePersonBase\" class=\"com.paml.test.SimplePerson\"

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">
    <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?


Try with the classpath*: prefix.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号