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

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,我在这就不给出了。

本人亲测,可以用的。有问题可以联系我,邮箱是dmj1161859184@126.com

 

 

 

转载于:https://my.oschina.net/u/2518341/blog/669547

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值