Spring实战——Bean的作用域request

一 配置

1 applicationContext.xml



    
    

2 web.xml



    
        org.springframework.web.context.ContextLoaderListener
    
    
        org.springframework.web.context.request.RequestContextListener
    

二 Bean

package org.crazyit.app.service;

public class Person
{
    private int age;
}

三 视图

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="org.springframework.web.context.*" %>
<%@ page import="org.springframework.web.context.support.*" %>
<%@ page import="org.crazyit.app.service.*"%>



    Spring Bean的作用域


<%
// 获取Web应用初始化的Spring容器
WebApplicationContext ctx =
    WebApplicationContextUtils.getWebApplicationContext(application);
// 两次获取容器中id为p的Bean
Person p1 = (Person)ctx.getBean("p");
Person p2 = (Person)ctx.getBean("p");
out.println((p1 == p2) + "
"); out.println(p1); %>

四 测试

Spring实战——Bean的作用域request_第1张图片

你可能感兴趣的:(spring,Spring)