我爱熊猫

最新评论

spring aop 之三利用aop实现池

spring默认是singleton的,在2.5中可以实现池,这样在获取bean实例时就可以从池中获取。

xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="groupUserTarget" class="cn.com.ultrapower.domain.GroupUser" scope="prototype">
</bean>
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="groupUserTarget"/>
<property name="maxSize" value="5"/>
</bean>

<bean id="groupUser" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource" ref="poolTargetSource"/>
</bean>

</beans>

这里GroupUser是一个模型,模拟某组用户,是一个普通的javabean。poolTargetSource中设置了maxSize,表示最大值是5。并且获取groupUser的方式是使用ProxyFactoryBean。

获取groupUser的方式如下:
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
// 通过池获取
GroupUser gu = (GroupUser)BeanFactory.getInstance().getBean("groupUser");
// 不通过池,直接获取,则每次都重新创建
// GroupUser gu = (GroupUser)BeanFactory.getInstance().getBean("groupUserTarget");
System.out.println(gu.toString());
}
}
在groupUser中使用

public groupUser(){

System.out.println("creat a groupUser");

}

这样就可以看到groupUser创建了几次。

posted on 2008-06-07 21:23 flyoo 阅读(61) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问