Spring4.04中實現bean的scope(範圍)設置爲session或者request

Sping中bean的scope的值能夠是singleton、prototype、request、session、global session。默認狀況下是singleton。java

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

下面我怎麼實現使用session或request的方法,不足之處請指出。spring

首先在eclipse中建一個web工程,總體架構以下圖所示:session

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">app

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

        </bean>
</beans>jsp

新建一個名爲UserServlet的Servlet,內容以下所示:ide

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;
}
}this

接下來是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

相關文章
相關標籤/搜索