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