java 将 spring bean 加载到 servlet 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6414373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:47:50  来源:igfitidea点击:

load spring bean into a servlet

javaspringjavabeansweb.xml

提问by ufk

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.

有很多关于如何完成这项任务的文档,但我仍然无法解决我的问题。我是 servlet 的新手,所以我可能错过了一些东西。

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.

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

when I point to red5 using port 5080 it acts as a regular tomcat server and i can browse jsp and servlet pages.

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

my web.xml contains the following relevant information:

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

my applicationContext.xml:

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

my FbAuth servlet:

我的 FbAuth servlet:

package com.xpogames;

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

}

I get the following error:

我收到以下错误:

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

I think I'm not fetching the spring bean correctly.

我想我没有正确获取春豆。

any ideas?

有任何想法吗?

thank you!

谢谢!

update

更新

this is my new init function:

这是我的新 init 函数:

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

thanks a lot! :)

多谢!:)

回答by Rajendra

I use the following in the init() method of the servlet. The init() method is called only once in a Servlets lifecycle.

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

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

Also, do you have the "contextConfigLocation" in your web.xml?

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

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

回答by Femi

You may have multiple versions of Spring on your class path: check that you aren't accidentally loading 2 different Spring jars.

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

回答by Nethaji Narasimalu

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