Spring get current ApplicationContext

后端 未结 11 885
陌清茗
陌清茗 2020-11-28 20:04

I am using Spring MVC for my web application. My beans are written in \"spring-servlet.xml\" file

Now I have a class MyClass and i want to

相关标签:
11条回答
  • 2020-11-28 20:30
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-servlet.xml");
    

    Then you can retrieve the bean:

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

    Reference: springbyexample.org

    0 讨论(0)
  • 2020-11-28 20:38

    Another way is to inject applicationContext through servlet.

    This is an example of how to inject dependencies when using Spring web services.

    <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>
    

    Alternate way is to add application Context in your web.xml as shown below

    <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>
    

    Basically you are trying to tell servlet that it should look for beans defined in these context files.

    0 讨论(0)
  • 2020-11-28 20:40

    based on Vivek's answer, but I think the following would be better:

    @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);
        }
    }
    

    Writing from an instance method to a static field is a bad practice and dangerous if multiple instances are being manipulated.

    0 讨论(0)
  • 2020-11-28 20:44

    If you're implementing a class that's not instantiated by Spring, like a JsonDeserializer you can use:

    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    MyClass myBean = context.getBean(MyClass.class);
    
    0 讨论(0)
  • 2020-11-28 20:46

    I think this link demonstrates the best way to get application context anywhere, even in the non-bean class. I find it very useful. Hope its the same for you. The below is the abstract code of it

    Create a new class 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;
        }
    }
    

    Add an entry in application-context.xml

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

    In annotations case (instead of application-context.xml)

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

    Get the context like this

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

    Cheers!!

    0 讨论(0)
  • 2020-11-28 20:47

    Add this to your code

    @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;
    

    This will simply inject myClass into your application

    0 讨论(0)
提交回复
热议问题