Java 如何从XML中指定父应用程序上下文?
我需要在springs中将已经存在的上下文重新用作父上下文 我有两个xml文件-bean.xml(父上下文),child-bean.xml(子上下文) 我能行Java 如何从XML中指定父应用程序上下文?,java,spring,Java,Spring,我需要在springs中将已经存在的上下文重新用作父上下文 我有两个xml文件-bean.xml(父上下文),child-bean.xml(子上下文) 我能行 ApplicationContext context2 = new ClassPathXmlApplicationContext(new String[] { "child-bean.xml" }, context); 但这种方法是通过代码实现的。 有没有办法通过XML来控制这一点 我试着把它放进child bean.xml <i
ApplicationContext context2 = new ClassPathXmlApplicationContext(new String[] { "child-bean.xml" }, context);
但这种方法是通过代码实现的。
有没有办法通过XML来控制这一点
我试着把它放进child bean.xml
<import resource="bean.xml"/>
但这不一样,因为看起来父级中的所有bean都在重新创建
TL;博士
我需要确保在创建子上下文时不会再次创建父上下文中bean的新实例。在不改变应用程序上下文初始化方式的情况下,是否可以通过XML实现这一点 在web.xml中,您可以使用以下方法加载多个应用程序上下文:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:bean1.xml,
classpath*:bean2.xml
</param-value>
</context-param>
上下文配置位置
类路径*:bean1.xml,
classpath*:bean2.xml
在我的例子中,我使用ref parent
我希望我的xml配置对您有所帮助
<!--parent -->
<bean id="abstractService" abstract="true"
class="com.lizjason.spring.AbstractService">
<property name="companyName"
value="lizjason"/>
</bean>
<bean id="shippingService"
parent="abstractService"
class="com.lizjason.spring.ShippingService">
<property name="shippedBy" value="lizjason"/>
</bean>
我这里说的不是父bean,而是父应用程序上下文我没有web.xml,它是一个独立的java应用程序。
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext container) throws ServletException{
XmlWEbApplicationContext applicationContext =
new XmlWebApplicationContext();
applicationContext.setConfigLocation("classpath:spring/*.xml");
container.addListener(new ContextLoaderListener(applicationContext));
}
}