2011-06-20 21 views
8

Có nhiều tài liệu về cách thực hiện nhiệm vụ này nhưng tôi vẫn không thể giải quyết được vấn đề của mình. Tôi mới làm việc với servlet vì vậy tôi có lẽ đã bỏ lỡ điều gì đó.tải đậu mùa xuân vào một servlet

Tôi sử dụng red5 sử dụng tomcat 6 để tạo một servlet sử dụng bean mùa xuân thuộc lớp MysqlDb cho thao tác cơ sở dữ liệu.

khi tôi trỏ tới red5 bằng cổng 5080, nó hoạt động như một máy chủ tomcat thông thường và tôi có thể duyệt các trang jsp và servlet.

web.xml của tôi chứa các thông tin liên quan như sau:

<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 tôi:

<?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 tôi servlet:

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

} 

tôi nhận được lỗi sau:

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

Tôi nghĩ rằng tôi sẽ không lấy đậu mùa xuân một cách chính xác.

bất kỳ ý tưởng nào?

cảm ơn bạn!

cập nhật

đây là hàm init mới của tôi:

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

thanks a lot! :)

Trả lời

9

Tôi sử dụng lệnh sau trong phương thức init() của servlet. Phương thức init() chỉ được gọi một lần trong vòng đời của Servlets.

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

Ngoài ra, bạn có "contextConfigLocation" trong tệp web.xml không?

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

thêm context-param và init (ServletConfig) đã khắc phục sự cố. cảm ơn rất nhiều! – ufk

2

Bạn có thể có nhiều phiên bản của Spring trên đường dẫn lớp học của mình: hãy kiểm tra xem bạn có vô tình tải 2 lọ Spring khác nhau không.

+0

Xin chào. Tôi đã kiểm tra và classpath của tôi chỉ chứa khung công tác Spring 3.0.5 – ufk

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