java中应用上下文_java-将ApplicationContext创建为Spring bean(通过其他应用程序上下文)...

我如何将一个ApplicationContext定义为其他应用程序上下文中的原型Spring bean.我还需要将当前上下文作为父级传递给新的应用程序上下文.

细节:

我有Bean,它表示富客户端应用程序中的一个用户会话.此类管理应用程序上下文和其他几个对象(例如数据库连接)的生命周期.该会话bean本身由特殊的“启动应用程序上下文”配置.

现在,我想对该会话bean进行单元测试,但是遇到麻烦,因为特定于会话的应用程序上下文是在会话bean中创建的,并且有很多依赖于“启动上下文”;

示例代码:

public class UserDBAminSession implements ApplicationContextAware, UserSession {

ApplicationContext startupContext;

ApplicationContext sessionContext;

public void setApplicationContext(ApplicationContext startupContext) {...}

public void start() {

createSessionContext() ;

}

private void createSessionContext() {

sessionContext = new ClassPathXmlApplicationContext("admin-session.xml", startupContext);

}

}

为了进行测试,我想使用以下代码重现createSessionContext函数代码:

sessionContext = startupContext.getBean("adminContext", ApplicationContext.class);

然后,我可以创建startupContext的模拟,它返回一些存根.甚至在将来的某个春天,甚至可以将DI的“会话上下文”传递给bean.但是,我不知道如何将父上下文参数传递给ClassPathXmlApplicationContext构造函数.我正在尝试类似的方法,但似乎不起作用:

scope="prototype" autowire="constructor">

admin-session.xml

我也在考虑在顶层创建应用程序上下文并通过setter传递它,但是:

>这只是将问题移到更高的水平,而不是解决.实际上,它已经完成了(UserSession-这是“顶层”).

>这打破了RAII模式.

>这需要大量的代码重构.

或制作特殊的“上下文工厂”对象,但现在已经不是最好的代码了.

看起来很蠢,我无法从IoC框架本身获得IoC对象.可能是我误读了一些春季文档吗?

还有其他想法,如何对该课程进行单元测试?

解决方法:

使用FactoryBean和ApplicationContextAware接口.

public class ChildApplicationContextFactoryBean implements FactoryBean, ApplicationContextAware {

protected String[] configLocations;

protected ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

@Override

public Object getObject() throws Exception {

return new ClassPathXmlApplicationContext(configLocations, applicationContext);

}

@Override

public Class getObjectType() {

return ClassPathXmlApplicationContext.class;

}

@Override

public boolean isSingleton() {

return true;

}

public void setConfigLocations(String[] configLocations) {

this.configLocations = configLocations;

}

}

用法:

applicationContext.xml

标签:spring,java

来源: https://codeday.me/bug/20191209/2097691.html

您可以按照以下步骤来给spring.xml文件配置应用程序上下文: 1. 在您的项目中创建一个名为“resources”的文件夹,并在其中创建一个名为“spring.xml”的文件。 2.spring.xml文件中定义应用程序上下文。例如,您可以使用以下代码: ``` <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 --> <bean id="exampleBean" class="com.example.ExampleBean"> <property name="exampleProperty" value="exampleValue" /> </bean> </beans> ``` 在这个例子中,我们定义了一个名为“exampleBean”的bean,它的类是“com.example.ExampleBean”,并设置了一个名为“exampleProperty”的属性值为“exampleValue”。 3. 在您的代码中,创建一个ApplicationContext对象,并将其初始化为spring.xml文件中定义的应用程序上下文。例如,您可以使用以下代码: ``` ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); ``` 这将创建一个基于类路径的应用程序上下文,并将其初始化为spring.xml文件中定义的上下文。 4. 现在您可以使用ApplicationContext对象来获取您在spring.xml文件中定义的bean。例如,您可以使用以下代码: ``` ExampleBean exampleBean = (ExampleBean) context.getBean("exampleBean"); ``` 这将获取名为“exampleBean”的bean,并将其强制转换为“com.example.ExampleBean”类型的对象。 这样,您就可以配置应用程序上下文并使用它来获取您在spring.xml文件中定义的bean了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值