Java Spring/在一个bean中访问当前的applicationContext';s工厂法
是否有可能获取当前Java Spring/在一个bean中访问当前的applicationContext';s工厂法,java,spring,scala,Java,Spring,Scala,是否有可能获取当前应用程序上下文的实际引用 package myPackage; public class AFactory { public static A createA() { // I need to access to the current instance of ApplicationContext here, passing it to another object's method for example }
应用程序上下文的实际引用
package myPackage;
public class AFactory {
public static A createA() {
// I need to access to the current instance of ApplicationContext here, passing it to another object's method for example
}
}
在myapplicationContext.xml
中:
<bean id="myBeanA" class="myPackage.AFactory" factory-method="createA">
</bean>
对于那些了解Scala的人,Spring在这里从逻辑上警告了这样一个事实:类中没有createActorSystem
static方法
有人处理过这种情况吗?让您的bean实现ApplicationContextAware
public class AFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
酷,只是测试一下:)我会让你知道没问题,我想我会验证你的答案,因为这是关于Java的好答案。然后为Scala发布另一个:)谢谢!
public class AFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}