java 如何从不在 Spring Container 中的类访问 Spring Bean 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28005761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a method a Spring Bean from a class not in the Spring Container
提问by Program-Me-Rev
I'm not a Springpro, so please bear with me....
我不是Spring专家,所以请多多包涵....
I have three classes:
我有三个班级:
class SpringBeanA {
public aMethod() {
.....
}
}
class SpringBeanB {
@Autowired SpringBeanA a;
public bMethod() {
a.method();
}
}
class NONSpringClass {
.....
b.method();
.....
}
b.method()
gives a null pointer error, both when accesed via the instances SpringBeanB b = new SpringBeanB()
and autowiring SpringBeanB to NONSpringClass.
b.method()
在通过实例访问SpringBeanB b = new SpringBeanB()
和将 SpringBeanB 自动装配到 NONSpringClass时,都会出现空指针错误。
The autowiring:
自动装配:
class NONSpringClass {
@Autowired SpringBeanB b;
.....
b.method();
.....
}
How can I successfully call b.method()
?
我怎样才能成功调用b.method()
?
回答by kamoor
Spring initializes all the objects and keep it in Spring Application Context. You have couple different ways to get access to objects inside Application context
Spring 初始化所有对象并将其保存在 Spring Application Context 中。您有几种不同的方法来访问应用程序上下文中的对象
First create a spring configuration class to inject ApplicationContext in to a private attribute and expose as static method.
首先创建一个 spring 配置类将 ApplicationContext 注入私有属性并公开为静态方法。
@Configuration
class StaticApplicationContext implements ApplicationContextAware{
static ApplicationContext applicationContext = null;
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
/**
* Note that this is a static method which expose ApplicationContext
**/
public static ApplicationContext getContext(){
return applicationContext;
}
}
Now you can try this in your non spring class,
现在你可以在你的非春季课程中尝试这个,
((SpringBeanB)StaticApplicationContext.getContext.getBean("b")).bMethod();
Please keep in mind, invoking getContext method before spring context initialization may cause NullPointerException. Also accessing beans outside of spring container is not a recommended approach. Ideal way will be to move all beans in to spring container to manage.
请记住,在 spring 上下文初始化之前调用 getContext 方法可能会导致 NullPointerException。此外,不推荐使用 spring 容器之外的 bean 访问。理想的方法是将所有 bean 移动到 spring 容器中进行管理。
If you want to access SpringApplicationContext from a java Servelet please refer WebApplicationContextUtils
如果你想从 java Servelet 访问 SpringApplicationContext 请参考WebApplicationContextUtils
回答by Davis Yoshida
A simple way to do this is using ApplicationContext.getBean().
一个简单的方法是使用 ApplicationContext.getBean()。
It's worth pointing out that this is considered bad practice, since it breaks inversion of control.
值得指出的是,这被认为是不好的做法,因为它破坏了控制反转。
回答by Luiggi Mendoza
Either you make Spring manage your NONSpringClass
and enable injection of SpringBeanB
in NONSpringClass
class or you have to manually inject a proper instance of SpringBeanB
in your NONSpringClass
reference. In the latter approach, Spring has nothing to do, and you manually have to create the necessary instances and inject them using setters.
要么你让春季管理NONSpringClass
,使注入SpringBeanB
的NONSpringClass
类或你必须手动注射的适当实例SpringBeanB
在你的NONSpringClass
参考。在后一种方法中,Spring 无事可做,您必须手动创建必要的实例并使用 setter 注入它们。