Spring4.04中实现bean的scope(范围)设置为session或者request
2016-05-04 00:00
148 查看
摘要: 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
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
相关文章推荐
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- 杰奇登录后的东西都是在session里面的
- @@IDENTITY与SCOPE_IDENTITY()
- ASP中SESSION无法保存问题的解决办法
- Oracle中的Connect/session和process的区别及关系介绍
- Node.js编程中客户端Session的使用详解
- 浅谈COOKIE和SESSION区别
- 解析PHP的session过期设置
- php中Session的生成机制、回收机制和存储机制探究
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- php中将一个对象保存到Session中的方法
- 深入解析Session是否必须依赖Cookie
- 对比分析php中Cookie与Session的异同
- PHP Session机制简介及用法
- 新手菜鸟必读:session与cookie的区别
- 深入讲解PHP Session及如何保持其不过期的方法
- PHP Session变量不能传送到下一页的解决方法
- PHP中的session永不过期的解决思路及实现方法分享
- php session实现多级目录存放实现代码