繁体   English   中英

Spring 获取当前的 ApplicationContext

[英]Spring get current ApplicationContext

我正在为我的 Web 应用程序使用 Spring MVC。 我的bean写在“ spring-servlet.xml ”文件中

现在我有一个MyClass类,我想使用 spring bean 访问这个类

spring-servlet.xml我写了以下

<bean id="myClass" class="com.lynas.MyClass" />

现在我需要使用ApplicationContext访问它

ApplicationContext context = ??

这样我才能做到

MyClass myClass = (MyClass) context.getBean("myClass");

这该怎么做??

只需注入它..

@Autowired
private ApplicationContext appContext;

或实现此接口: ApplicationContextAware

我认为此 链接演示了在任何地方获取应用程序上下文的最佳方法,即使在非 bean 类中也是如此。 我觉得它非常有用。 希望你也一样。 下面是它的抽象代码

创建一个新类 ApplicationContextProvider.java

package com.java2novice.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware{

    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac)
            throws BeansException {
        context = ac;
    }
}

在 application-context.xml 中添加一个条目

<bean id="applicationContextProvider"
                        class="com.java2novice.spring.ApplicationContextProvider"/>

在注释情况下(而不是 application-context.xml)

@Component
public class ApplicationContextProvider implements ApplicationContextAware{
...
}

获取这样的上下文

TestBean tb = ApplicationContextProvider.getApplicationContext().getBean("testBean", TestBean.class);

干杯!!

如果您需要从自身未由 Spring 实例化HttpServlet 中访问上下文(因此 @Autowire 和 ApplicationContextAware 都不起作用)...

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

或者

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

至于其他一些回复,请在执行此操作之前三思:

new ClassPathXmlApplicationContext("..."); // are you sure?

...因为这不会为您提供当前上下文,而是为您创建了另一个实例。 这意味着 1) 大量内存和 2) bean 在这两个应用程序上下文之间不共享。

如果您正在实现一个不是由 Spring 实例化的类,例如 JsonDeserializer,您可以使用:

WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
MyClass myBean = context.getBean(MyClass.class);

将此添加到您的代码中

@Autowired
private ApplicationContext _applicationContext;

//Add below line in your calling method
MyClass class = (MyClass) _applicationContext.getBean("myClass");

// Or you can simply use this, put the below code in your controller data member declaration part.
@Autowired
private MyClass myClass;

这将简单地将 myClass 注入您的应用程序

基于 Vivek 的回答,但我认为以下内容会更好:

@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {

    private static class AplicationContextHolder{

        private static final InnerContextResource CONTEXT_PROV = new InnerContextResource();

        private AplicationContextHolder() {
            super();
        }
    }

    private static final class InnerContextResource {

        private ApplicationContext context;

        private InnerContextResource(){
            super();
        }

        private void setContext(ApplicationContext context){
            this.context = context;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return AplicationContextHolder.CONTEXT_PROV.context;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) {
        AplicationContextHolder.CONTEXT_PROV.setContext(ac);
    }
}

如果操作多个实例,从实例方法写入静态字段是一种不好的做法并且很危险。

在 Spring 应用程序中有多种获取应用程序上下文的方法。 这些是在下面给出的:

  1. 通过 ApplicationContextAware

     import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class AppContextProvider implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }

这里setApplicationContext(ApplicationContext applicationContext)方法你会得到applicationContext

  1. 通过自动连线

     @Autowired private ApplicationContext applicationContext;

这里@Autowired关键字将提供applicationContext。

有关更多信息,请访问此线程

谢谢 :)

第一步:在类中注入以下代码

@Autowired
private ApplicationContext _applicationContext;

第 2 步:编写 Getter 和 Setter

第 3 步:在定义 bean 的 xml 文件中定义 autowire="byType"

另一种方式是通过servlet注入applicationContext。

这是使用 Spring Web 服务时如何注入依赖项的示例。

<servlet>
        <servlet-name>my-soap-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:my-applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>5</load-on-startup>

</servlet>

另一种方法是在 web.xml 中添加应用程序上下文,如下所示

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/classes/my-another-applicationContext.xml
        classpath:my-second-context.xml
    </param-value>
</context-param>

基本上,您试图告诉 servlet 它应该查找在这些上下文文件中定义的 bean。

即使在添加 @Autowire 之后,如果您的类不是 RestController 或 Configuration Class,applicationContext 对象也会为空。 尝试使用下面的创建新类,它工作正常:

@Component
public class SpringContext implements ApplicationContextAware{

   private static ApplicationContext applicationContext;

   @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws 
     BeansException {
    this.applicationContext=applicationContext;
   }
 }

然后,您可以根据需要在同一个类中实现一个 getter 方法,例如通过以下方式获取已实现的类引用:

    applicationContext.getBean(String serviceName,Interface.Class)

比使用 @Autowired 更好的是让它通过构造函数注入。 在这里找到一些参数专业构造函数注入

@Component
public class MyClass{
  private final ApplicationContext applicationContext;

  public MyClass(ApplicationContext applicationContext){
    this.applicationContext = applicationContext;
  }

  //here will be your methods using the applicationcontext
}

我正在将Spring MVC用于我的Web应用程序。 我的bean是写在“ spring-servlet.xml ”文件中的

现在我有一个类MyClass ,我想用spring bean访问这个类。

spring-servlet.xml我写了以下内容

<bean id="myClass" class="com.lynas.MyClass" />

现在我需要使用ApplicationContext访问它

ApplicationContext context = ??

这样我就可以

MyClass myClass = (MyClass) context.getBean("myClass");

这该怎么做??

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM