将spring bean加载到servlet中

时间:2011-06-20 16:21:47

标签: java spring javabeans web.xml

有很多关于如何完成此任务的文件,但我仍然无法解决我的问题。我刚接触servlet,所以我可能错过了一些东西。

我使用red5使用tomcat 6来创建一个servlet,它使用一个用于数据库操作的MysqlDb类的spring bean。

当我使用端口5080指向red5时,它充当常规的tomcat服务器,我可以浏览jsp和servlet页面。

我的web.xml包含以下相关信息:

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

<servlet>
  <servlet-name>fbauth</servlet-name>
  <servlet-class>com.xpogames.FbAuth</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>fbauth</servlet-name>
  <url-pattern>/fbauth</url-pattern>
</servlet-mapping>

我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/red5-web.properties" />
</bean>

<bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${db.driver}</value></property>
    <property name="url"><value>${db.url}</value></property>
    <property name="username"><value>${db.username}</value></property>
    <property name="password"><value>${db.password}</value></property>
    <property name="poolPreparedStatements"><value>true</value></property>
    <property name="maxActive"><value>10</value></property>
    <property name="maxIdle"><value>10</value></property>
 </bean>

 <bean id="MysqlDb" class="com.xpogames.MysqlDb">
    <property name="idDataSource" ref="idDataSource"/>
 </bean>


</beans>

我的FbAuth servlet:

package com.xpogames;

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("MysqlDb");
    out.println(mysqlDb.testme());
    out.println("Hello, world!");
    out.close();        
}

}

我收到以下错误:

java.lang.IllegalStateException: Context attribute is not of type WebApplicationContext

我想我没有正确地获取spring bean。

任何想法?

谢谢你!

更新

这是我的新init函数:

public void init(ServletConfig config) throws ServletException {
    super.init(config);
     ServletContext servletContext = this.getServletContext();
    this._context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}

非常感谢! :)

3 个答案:

答案 0 :(得分:10)

我在servlet的init()方法中使用以下内容。 init()方法在Servlets生命周期中只调用一次。

ApplicationContext context = 
    WebApplicationContextUtils.getRequiredWebApplicationContext(
        this.getServletContext());

另外,你的web.xml中有“contextConfigLocation”吗?

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/**/spring-config.xml
        </param-value>
 </context-param>

答案 1 :(得分:2)

您的类路径上可能有多个版本的Spring:检查您是否意外加载了2个不同的Spring jar。

答案 2 :(得分:1)

 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
webApplicationContext .getBean("Name")