java file upload - Access Spring beans from a servlet in JBoss

2 Answers

Your servlet can use WebApplicationContextUtils to get the application context, but then your servlet code will have a direct dependency on the Spring Framework.

Another solution is configure the application context to export the Spring bean to the servlet context as an attribute:

<bean class="">
  <property name="attributes">
      <entry key="jobbie" value-ref="springifiedJobbie"/>

Your servlet can retrieve the bean from the servlet context using

SpringifiedJobbie jobbie = (SpringifiedJobbie) getServletContext().getAttribute("jobbie");
and download

I want to write a simple servlet in JBoss which will call a method on a Spring bean. The purpose is to allow a user to kick off an internal job by hitting a URL.

What is the easiest way to get hold of a reference to my Spring bean in the servlet?

JBoss web services allow you to inject a WebServiceContext into your service class using an @Resource annotation. Is there anything comparable that works in plain servlets? A web service to solve this particular problem would be using a sledgehammer to crush a nut.

I've found one way to do it:

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
SpringifiedJobbie jobbie = (SpringifiedJobbie)context.getBean("springifiedJobbie");