title: load spring bean into a servlet
body: <p>There are many documentations out there on how to achieve this task but I still couldn't resolve my issue. I'm new to working with servlet so I probably missed something.</p>
  <p>I use red5 that uses tomcat 6 to create a servlet that uses a spring bean that's of a MysqlDb class for database manipulations.</p>
  <p>when I point to red5 using port 5080 it acts as a regular tomcat server and i can browse jsp and servlet pages.</p>
  <p>my web.xml contains the following relevant information:</p>
  <pre><code>&lt;listener&gt;
   &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
    &lt;servlet-name&gt;fbauth&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.xpogames.FbAuth&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;fbauth&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/fbauth&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  <p>my applicationContext.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
     xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
     xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;&gt;
  
  &lt;bean id=&quot;placeholderConfig&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;
      &lt;property name=&quot;location&quot; value=&quot;/WEB-INF/red5-web.properties&quot; /&gt;
  &lt;/bean&gt;
  
  &lt;bean id=&quot;idDataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;
      &lt;property name=&quot;driverClassName&quot;&gt;&lt;value&gt;${db.driver}&lt;/value&gt;&lt;/property&gt;
      &lt;property name=&quot;url&quot;&gt;&lt;value&gt;${db.url}&lt;/value&gt;&lt;/property&gt;
      &lt;property name=&quot;username&quot;&gt;&lt;value&gt;${db.username}&lt;/value&gt;&lt;/property&gt;
      &lt;property name=&quot;password&quot;&gt;&lt;value&gt;${db.password}&lt;/value&gt;&lt;/property&gt;
      &lt;property name=&quot;poolPreparedStatements&quot;&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
      &lt;property name=&quot;maxActive&quot;&gt;&lt;value&gt;10&lt;/value&gt;&lt;/property&gt;
      &lt;property name=&quot;maxIdle&quot;&gt;&lt;value&gt;10&lt;/value&gt;&lt;/property&gt;
   &lt;/bean&gt;
   
   &lt;bean id=&quot;MysqlDb&quot; class=&quot;com.xpogames.MysqlDb&quot;&gt;
      &lt;property name=&quot;idDataSource&quot; ref=&quot;idDataSource&quot;/&gt;
   &lt;/bean&gt;
   
  
  &lt;/beans&gt;
  </code></pre>
  <p>my FbAuth servlet:</p>
  <p>package com.xpogames;</p>
  <pre><code>import java.io.IOException;
  
  import java.io.PrintWriter;
  
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.context.ApplicationContext;
  import org.springframework.web.context.WebApplicationContext;
  import org.springframework.web.context.support.WebApplicationContextUtils;
  
  public class FbAuth extends HttpServlet {
  private static final long serialVersionUID = 1L;
     
  public FbAuth() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      MysqlDb mysqlDb = (MysqlDb)webApplicationContext.getBean(&quot;MysqlDb&quot;);
      out.println(mysqlDb.testme());
      out.println(&quot;Hello, world!&quot;);
      out.close();        
  }
  
  }
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>java.lang.IllegalStateException: Context attribute is not of type WebApplicationContext
  </code></pre>
  <p>I think I'm not fetching the spring bean correctly.</p>
  <p>any ideas?</p>
  <p>thank you!</p>
  <h1>update</h1>
  <p>this is my new init function:</p>
  <pre><code>public void init(ServletConfig config) throws ServletException {
      super.init(config);
       ServletContext servletContext = this.getServletContext();
      this._context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
  }
  </code></pre>
  <p>thanks a lot! :)</p>