Question

I have a big java project that contains many servlets. and each servlet needs to get objects from the same bean file using the following command:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

and then I use

     context.getBean("<BEAN_NAME">);

some of them even needs to get same objects.

the question is if it's possible to inject the object that I want directly to the servlets without reading the bean file manually.

each servlet is configured in web.xml.

any information regarding the issue would be greatly appreciated!

thanks

Was it helpful?

Solution 2

Since you have a web application, I would go with a WebApplicationContext contained in spring-web, which is loaded at deploy-time of your web application and properly closed on undeploy. All you have to do is declaring the ContextLoaderListener in your web.xml:

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

Then you can access your ApplicationContext from any servlet:

ApplicationContext ctx = WebApplicationContextUtils
    .getRequiredWebApplicationContext(getServletContext());
MyBean myBean = (MyBean) ctx.getBean("myBean");
myBean.doSomething();

The advantage is, that your context is shared between all servlets.

References:

OTHER TIPS

Have you considered having your servlets implement HttpRequestHandler?

You then have to declare your Servlet as named Spring bean and use the same name on web.xml, then you can simply use the @Autowired annotation to injection your Spring beans in your Servlet

More info at http://www.codeproject.com/Tips/251636/How-to-inject-Spring-beans-into-Servlets

Sample Code:


  @Component("myServlet") 
   public class MyServlet implements HttpRequestHandler {

        @Autowired
        private MyService myService;
...

sample web.xml

<servlet>     
            <display-name>MyServlet</display-name>
            <servlet-name>myServlet</servlet-name>
            <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet
           </servlet-class>
    </servlet>
    <servlet-mapping>
            <servlet-name>myServlet</servlet-name>
            <url-pattern>/myurl</url-pattern>
    </servlet-mapping>

You can use a SerlvetContextListener with the Servlet#init() method. When your servlet container creates a servlet context, it will call contextInitialized() on any ServletContextListeners where you can do initialization of application singletons/beans/etc.

public class YourServletContextListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // clear context
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // initialize spring context    
        event.getServletContext().setAttribute("context", springContext);
    }
}

All Servlets within this context (servlet context) will have access to these attributes.

In the Servlet init() method, you just get the attribute

public class YourServlet implements Servlet {

    @Override
    public void init(ServletConfig config) {
        config.getServletContext().getAttribute("context");
        // cast it (the method returns Object) and use it
    }

    // more
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top