前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring4.04中实现bean的scope(范围)设置为session或者request

Spring4.04中实现bean的scope(范围)设置为session或者request

作者头像
克虏伯
发布2019-04-15 15:11:02
1K0
发布2019-04-15 15:11:02
举报

Sping中bean的scope的值可以是singleton、prototype、request、session、global session。默认情况下是singleton。

只有在web容器中才能使用request、session、global session。

下面我怎么实现使用session或request的方法,不足之处请指出。

首先在eclipse中建一个web工程,整体架构如下图所示:

Spring的配置文件Spring-config.xml中的内容如下所示,下面的userService1的scope的值为session:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd">     <context:annotation-config/>         <bean id="userDao1" class="com.yun.project.dao.UserDao" scope="prototype">         </bean>         <bean id="userService1" class="com.yun.project.service.UserService" scope="session">

            <property name="userDao" ref="userDao1"/> 

        </bean> </beans>

新建一个名为UserServlet的Servlet,内容如下所示:

package com.yun.project.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.yun.project.service.UserService; import com.yun.project.util.Out;//Out是我将System.out.println()给封装了 //即Out.println(String)等价于System.out.println(String) public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doGet(req, resp); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doPost(req, resp); Out.println("到doPost了"); UserService userService = (UserService) getBean("userService1");//取出Spring中的userService1 userService.sayHello(); Out.println("这是learnSpring1中的"); } private Object getBean(String beanId){ ServletContext servletContext = this.getServletContext();  Out.println(servletContext==null?"是null":"不是null"); ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);   Object myDao = context.getBean(beanId);   return myDao; } }

接下来是web.xml中的配置,内容如下:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">   <display-name>LearnSpring1</display-name>   <welcome-file-list>     <welcome-file>index.jsp</welcome-file>   </welcome-file-list>   <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>  classpath*:Spring*.xml  </param-value>    </context-param>     <listener>     <listener-class>              org.springframework.web.context.ContextLoaderListener          </listener-class>   </listener>   <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>   </listener>   <servlet>   <servlet-name>user</servlet-name>   <servlet-class>com.yun.project.servlet.UserServlet</servlet-class>   </servlet>   <servlet-mapping>   <servlet-name>user</servlet-name>   <url-pattern>/user</url-pattern>   </servlet-mapping> </web-app>

注,我没有给出userService的sayHello(),不过这没有涉及到spring的配置了,随便你怎么写。

需要的jar,我在这就不给出了。

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016/05/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档