2011-06-20 8 views
8

Existen muchos documentos sobre cómo lograr esta tarea, pero aún no pude resolver mi problema. Soy nuevo en el trabajo con servlet, así que probablemente me haya perdido algo.carga de bean de resorte en un servlet

Uso red5 que usa tomcat 6 para crear un servlet que utiliza un bean de resorte que pertenece a una clase MysqlDb para manipulaciones de base de datos.

cuando señalo a red5 utilizando el puerto 5080, actúa como un servidor regular de tomcat y puedo navegar por las páginas jsp y servlet.

mi web.xml contiene la siguiente información relevante:

<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> 

mi 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> 

mi FbAuth servlet:

com.xpogames del paquete;

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();   
} 

} 

me sale el siguiente error:

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

Creo que no voy a ir a buscar el grano de la primavera correctamente.

¿Alguna idea?

gracias!

actualización

esta es mi nueva función init:

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

muchas gracias! :)

Respuesta

9

Utilizo lo siguiente en el método init() del servlet. El método init() se llama solo una vez en un ciclo de vida de Servlets.

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

Además, ¿tiene el "contextConfigLocation" en su web.xml?

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

al agregar context-param y al reemplazar el init (ServletConfig) corrigió el problema. ¡muchas gracias! – ufk

2

Puede tener múltiples versiones de Spring en su ruta de clase: compruebe que no está cargando accidentalmente 2 jarras Spring diferentes.

+0

hola. Comprobé y mi classpath contiene solo el marco de primavera 3.0.5 – ufk

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