我想使用Spring在servlet中注入一个对象

 秋忆道格 发布于 2023-02-04 15:38

我在我的应用程序中有两个servlet,我希望将一个A类对象注入到两个servlet中,我也希望在整个应用程序中使用相同的ApplicationContext,即在SO的这个问题的第一个答案中提到的两个servlet: Spring注入Servlet

现在我经历了很多像这样的问题,但找不到与我的问题相符的东西.为了更好地解释,我在这里写一个粗略的代码

public class servletOne extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }
}

public class servletTwo extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }
}

所以上面是applicationContext.xml中的两个服务器我想将一个对象传递给这两个servlet,因此按照惯例,我想要一个像这样的功能:


        




        






    

我不知道这是否可能,我是春天的新手,我只有依赖注入的基本知识.

如果有人能帮我这个,我真的很感激.这将清除我的许多疑虑并帮助我在学习Spring的过程中前进.

这是web.xml

    
        contextConfigLocation
        /WEB-INF/applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
        2
    
    
        servletOne
        mypackage.servletOne
    

        servletTwo
        mypackage.servletTwo
    
    
        dispatcher
        *.htm
    
    
        servletOne
        /servletOne
    
    
        servletTwo
        /servletTwo
    

    
        
            300
        
    

Sotirios Del.. 15

你混淆了两个概念:Servlets和Spring ApplicationContext.Servlet由Servlet容器管理,让我们以Tomcat为例.在ApplicationContext由Spring管理.

Servlet在部署描述符中声明为a 时


    servletOne
    mypackage.servletOne


    servletOne
    /servletOne

Servlet容器将创建您的mypackage.servletOne类的实例,注册它,并使用它来处理请求.这就是它对DispatcherServletSpring MVC的基础所做的.

Spring是一个ApplicationContext用于管理多个bean 的IoC容器.该ContextLoaderListener负载根ApplicationContext(无论从任何位置,你告诉它).在DispatcherServlet使用了根上下文,也必须加载自己的.上下文必须具有适当的配置DispatcherServlet才能工作.

在Spring上下文中声明一个bean,比如


        

无论它与web.xml中声明的类型相同,都是完全不相关的.上面的bean与web.xml中的声明无关.

正如我在这里的回答一样,因为ContextLoaderListener它将ApplicationContext它创建ServletContext的属性作为一个属性,ApplicationContext可供任何Servlet容器管理对象使用.因此,您可以覆盖HttpServlet#init(ServletConfig)自定义HttpServlet类,如此

@Override
public void init(ServletConfig config) throws ServletException {
   super.init(config);

   ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

   this.someObject = (SomeBean)ac.getBean("someBeanRef");
}

假设你的root ApplicationContext包含一个名为的bean someBeanRef.

还有其他替代方案.例如,这.

1 个回答
  • 你混淆了两个概念:Servlets和Spring ApplicationContext.Servlet由Servlet容器管理,让我们以Tomcat为例.在ApplicationContext由Spring管理.

    Servlet在部署描述符中声明为a 时

    <servlet>
        <servlet-name>servletOne</servlet-name>
        <servlet-class>mypackage.servletOne</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>servletOne</servlet-name>
        <url-pattern>/servletOne</url-pattern>
    </servlet-mapping>
    

    Servlet容器将创建您的mypackage.servletOne类的实例,注册它,并使用它来处理请求.这就是它对DispatcherServletSpring MVC的基础所做的.

    Spring是一个ApplicationContext用于管理多个bean 的IoC容器.该ContextLoaderListener负载根ApplicationContext(无论从任何位置,你告诉它).在DispatcherServlet使用了根上下文,也必须加载自己的.上下文必须具有适当的配置DispatcherServlet才能工作.

    在Spring上下文中声明一个bean,比如

    <bean id="servletFirst" class="mypackage.servletOne">
            <property name="message" ref="classObject" />
    </bean>
    

    无论它与<servlet>web.xml中声明的类型相同,都是完全不相关的.上面的bean与<servlet>web.xml中的声明无关.

    正如我在这里的回答一样,因为ContextLoaderListener它将ApplicationContext它创建ServletContext的属性作为一个属性,ApplicationContext可供任何Servlet容器管理对象使用.因此,您可以覆盖HttpServlet#init(ServletConfig)自定义HttpServlet类,如此

    @Override
    public void init(ServletConfig config) throws ServletException {
       super.init(config);
    
       ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    
       this.someObject = (SomeBean)ac.getBean("someBeanRef");
    }
    

    假设你的root ApplicationContext包含一个名为的bean someBeanRef.

    还有其他替代方案.例如,这.

    2023-02-04 15:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有