0

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

ufk
  • 30,912
  • 70
  • 235
  • 386

3 Answers3

2

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>
Rafael Oltra
  • 1,239
  • 9
  • 15
  • Wouldn't you just use a `DispatcherServlet` and the full Spring MVC suite then? – Sotirios Delimanolis Jul 10 '13 at 14:10
  • yeah i googled that first before posting the question. had problems understanding that AutoWired functionality. i'm used to injecting with xml configuration. not with code annotations. – ufk Jul 10 '13 at 14:11
1

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
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • with @nif and your answer i can come up with some kind of a solution. checking it now. thanks – ufk Jul 10 '13 at 14:13
1

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:

Community
  • 1
  • 1
nif
  • 3,342
  • 20
  • 18
  • i don't understand what is the MyBeans class. before i just use ApplicationContext to get the bean. now I need to create a class for it ? – ufk Jul 10 '13 at 14:36
  • or maybe i'm missing something.. ctx.getBean gets the bean itself and not the bean.xml file ? – ufk Jul 10 '13 at 14:42
  • ahah ok.. so how do i tell it exactly to which bean file to go to ? – ufk Jul 10 '13 at 14:43
  • @ufk: I edited the answer to include that part from Spring documentation. Specify the location of the XML/XMLs via `contextConfigLocation` `context-param`. – nif Jul 10 '13 at 15:29
  • ohhhh not as complicated as i thought it would be. – ufk Jul 10 '13 at 15:42