---
title: "spring <bean scope=\"prototype\">探讨"
author: "fireq3"
date: "2013-08-17T13:13:23Z"
tags: []
categories: ["编程语言"]
slug: "spring-bean-scope-prototype"
draft: false
lastmod: "2013-08-17T13:13:23Z"
id: 1926437
url: "https://fireq3.iteye.com/blog/1926437"
---

在配置spring applicationContext.xml中 

&lt;bean id="person"  class="com.zcq.model.Person"  scope="prototype"&gt;&lt;/bean&gt;  
       
     &lt;bean id="serviceImp" class="com.zcq.serviceImp.ServiceImp"&gt;&lt;/bean&gt;

 

scope="prototype"没写的问题,项目中对一个表的增删该操作是用一个action，这个action有add,update,delete,save这些方法，  
 添加和修改是共用一个页面，当页面得到id时代表进行的修改操作，反之是添加操作。因为在配置spring的bean是忘了写scope="prototype"  
 所以每次添加时都显示最后一次访问过的记录,找了很长时间，原来是spring bean出了问题。 scope="prototype" 会在该类型的对象被请求  
 时创建一个新的action对象。如果没有配置scope=prototype则添加的时候不会新建一个action，他任然会保留上次访问的过记录的信息