복붙노트

[SPRING] 스프링 빈을 서블릿에로드한다.

SPRING

스프링 빈을 서블릿에로드한다.

이 작업을 수행하는 방법에 대한 많은 문서가 있지만 문제는 해결할 수 없습니다. 나는 서블릿에 익숙하지 않으므로 아마도 뭔가를 놓쳤을 것이다.

Tomcat 6을 사용하는 red5를 사용하여 데이터베이스 조작을위한 MysqlDb 클래스의 스프링 빈을 사용하는 서블릿을 작성합니다.

내가 포트 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 서블릿 :

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

나는 봄 콩을 올바르게 가져 오지 않는다고 생각한다.

어떤 아이디어?

고맙습니다!

이것은 나의 새로운 init 함수이다.

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

고마워요! :)

해결법

  1. ==============================

    1.나는 서블릿의 init () 메소드에서 다음을 사용한다. init () 메소드는 서블릿 라이프 사이클에서 한 번만 호출됩니다.

    나는 서블릿의 init () 메소드에서 다음을 사용한다. init () 메소드는 서블릿 라이프 사이클에서 한 번만 호출됩니다.

    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>
    
  2. ==============================

    2.클래스 경로에 여러 버전의 Spring이있을 수 있습니다 : 두 개의 다른 Spring jar를 실수로로드하지 않았는지 확인하십시오.

    클래스 경로에 여러 버전의 Spring이있을 수 있습니다 : 두 개의 다른 Spring jar를 실수로로드하지 않았는지 확인하십시오.

  3. ==============================

    3.

     WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    webApplicationContext .getBean("Name")
    
  4. from https://stackoverflow.com/questions/6414373/load-spring-bean-into-a-servlet by cc-by-sa and MIT license