Spring實(shí)戰(zhàn)之Bean的作用域request用法分析
本文實(shí)例講述了Spring實(shí)戰(zhàn)之Bean的作用域request用法。分享給大家供大家參考,具體如下:
一 配置
1 applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 指定使用request作用域 -->
<bean id="p" class="org.crazyit.app.service.Person"
scope="request"/>
</beans>
2 web.xml
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
二 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.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Spring Bean的作用域</title>
</head>
<body>
<%
// 獲取Web應(yīng)用初始化的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) + "<br/>");
out.println(p1);
%>
</body>
</html>
四 測試

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
- 詳解Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式
- 淺談Spring中Bean的作用域、生命周期
- Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法分析
- 深入了解Spring中Bean的作用域和生命周期
- spring ioc的簡單實(shí)例及bean的作用域?qū)傩越馕?/a>
- JSP 中Spring Bean 的作用域詳解
- 簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
- Spring中Bean的作用域與生命周期詳解
- 詳解Spring中Bean的作用域與生命周期
- Spring?框架中的?Bean?作用域(Scope)使用詳解
相關(guān)文章
java日期格式化YYYY-MM-dd遇坑指南小結(jié)
本文主要介紹了java日期格式化YYYY-MM-dd遇坑指南小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
druid執(zhí)行SQL出現(xiàn)錯(cuò)誤但不影響返回結(jié)果的問題及解決
這篇文章主要介紹了druid執(zhí)行SQL出現(xiàn)錯(cuò)誤但不影響返回結(jié)果的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
關(guān)于@Component注解下的類無法@Autowired問題
這篇文章主要介紹了關(guān)于@Component注解下的類無法@Autowired問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
集合嵌套之ArrayList嵌套ArrayList實(shí)例
下面小編就為大家?guī)硪黄锨短字瓵rrayList嵌套ArrayList實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例
本篇文章主要介紹了在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02

