我们正在使用spring 3.2。
我们在myAccountVO
个文件中定义了一个bean spring.xml
,并将范围设置为prototype
,但是spring将这个bean创建为singleton bean。
这是spring xml:
<bean name="myAccountVO1"
class="valueobject.AccountVO"
scope="prototype" >
<property name="accountNo" value="0105069413007" />
<property name="accountType" value="01" />
</bean>
服务类是:
@Service //I've tested the @Scope("prototype") but no luck
public class AccountSummary {
@Autowired //I also tested @Resource but same result
private AccountSummaryVO myAccountSummaryVO1;
AccountSummaryVO getAccount(){
return myAccountSummaryVO1
}
}
稍后我们将此服务用作:
@Autowired
AccountSummary accountSummary;
............
accountSummary.getAccount()
就我而言,AccountSummary
类本身就是一个单例,每次都不会被实例化。
这似乎是非常基本的用例,但我不知道我错过了什么。
答案 0 :(得分:1)
我看不到你注射myAccountVO1
的位置。
但是我想当你揭示注入的地方时,它可能是一个豆子的成员,而这个豆子本身不在范围原型中,例如@Service或@Controller。服务bean将使用新创建的myAccountVO1
进行实例化,但此实例将永远保留在那里。
更改包含bean的范围。请参阅4.5.3 Singleton beans with prototype-bean dependencies。
这也适用于注入了服务bean的bean。