标签:exce 系统 when cep 问题 factor nfa 状态 ant
<bean id="user" class="modle.User" scope="prototype"> </bean>
现在又这么一种情况.
User类调用一个service, 这个service又调用一个tool。
有时我们希望User是多例的,service是单例的,而tool又是多例的。
<bean id="user" class="modle.User" scope="prototype"> <property name="service" ref="userservice"></property> </bean> <bean id="userservice" class="service.userService" > <property name="tool" ref="tool"></property> </bean> <bean id="tool" class="service.ToolImpl" scope="prototype"> </bean>
public class userService implements BeanFactoryAware{ private Tool tool; private BeanFactory factory; public void service(){ this.tool = (Tool)factory.getBean("tool"); System.out.println(this+":service"); tool.work(); } public Tool getTool() { return tool; } // public void setTool(Tool tool) {// // this.tool = (Tool)factory.getBean("tool");// } public void setBeanFactory(BeanFactory f) throws BeansException { factory = f; } }
配置文件,不能再使用注入。因此要把tool对象的注入去掉!
<bean id="user" class="modle.User" scope="prototype"> <property name="service" ref="userservice"></property> </bean> <bean id="userservice" class="service.userService" > </bean> <bean id="tool" class="service.ToolImpl" scope="prototype"> </bean>
public interface Tool { public void work(); } public class ToolImpl implements Tool{ public void work() { System.out.println(this+":Tool Work"); } }
测试类:
public class Test { public static void main(String[] args) { ClassPathResource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory factory = new XmlBeanFactory(res); User user = (User)factory.getBean("user"); User user2 = (User)factory.getBean("user"); System.out.println(user); user.getService().service(); System.out.println(); System.out.println(user2); user2.getService().service(); } }
Output:
modle.User@42552c service.userService@19e15c:service service.ToolImpl@11a75a2:Tool Work
modle.User@210b5b service.userService@19e15c:service service.ToolImpl@170888e:Tool Work
标签:exce 系统 when cep 问题 factor nfa 状态 ant
原文地址:http://www.cnblogs.com/ggds/p/7855824.html