Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring/在一个bean中访问当前的applicationContext';s工厂法_Java_Spring_Scala - Fatal编程技术网

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 
       }

    }
在my
applicationContext.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;
    }
}