java – 如何在Spring中池对象?

前端之家收集整理的这篇文章主要介绍了java – 如何在Spring中池对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在关注如何在 Spring中汇集对象的这个 tutorial.我按照教程中的说明进行操作,但是当我运行我的应用程序时,它总是会生成一个新的对象实例.我期待,因为我正在汇集对象,现有的对象将被重用.因此,不应创建新实例.此外,当我访问bean的getter方法时,会再次创建bean的新实例.

我怎么可能做错了?我是否误解了Spring中汇集的概念?

以下是我的代码

应用程序上下文:(这只是我的应用程序上下文的主体.)

  1. <bean id="simpleBeanTarget" class="com.bean.SimpleBean" scope="prototype">
  2.  
  3. </bean>
  4.  
  5. <bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
  6. <property name="targetBeanName" value="simpleBeanTarget" />
  7. <property name="maxSize" value="2" />
  8. </bean>
  9.  
  10. <bean id="simpleBean" class="org.springframework.aop.framework.Proxyfactorybean">
  11. <property name="targetSource" ref="poolTargetSource" />
  12. </bean>

控制器:(这只是我方法的主体)

  1. @RequestMapping("/hello")
  2. public ModelAndView helloWorld(HttpServletRequest request,HttpServletResponse response)
  3. {
  4. String message = "Hello World,Spring 3.";
  5. try
  6. {
  7. System.out.println("Accessing Application Context");
  8. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  9.  
  10. System.out.println("Getting Bean");
  11. SimpleBean simpleBean = (SimpleBean) context.getBean("simpleBean");
  12. //A new SimpleBean... is printed here.
  13.  
  14. System.out.println("Displaying Hello World: " + simpleBean.getRandomNum());
  15. //After this line,A new SimpleBean... is printed again. I simply access the getter method. Why does it create a new instance of SimpleBean?
  16.  
  17. return new ModelAndView("hello","message",message);
  18. }catch(Exception e)
  19. {
  20. System.out.println("Error: " + e);
  21. e.printStackTrace();
  22. return new ModelAndView("hello","Error! " + e.getMessage());
  23. }
  24. }

我正在汇集的bean:

  1. package com.bean;
  2.  
  3. import java.util.Random;
  4.  
  5. public class SimpleBean
  6. {
  7. int randomNum;
  8. String str;
  9.  
  10. SimpleBean()
  11. {
  12. Random randomGenerator = new Random();
  13. randomNum = randomGenerator.nextInt(100);
  14.  
  15. //I'm printing this line just to check if a instance of this bean is created.
  16. System.out.println("#####################A new SimpleBean was born: " + randomNum);
  17.  
  18. str = "This is a string.";
  19. }
  20.  
  21. public int getRandomNum()
  22. {
  23. return randomNum;
  24. }
  25.  
  26. public void setRandomNum(int randomNum)
  27. {
  28. this.randomNum = randomNum;
  29. }
  30.  
  31. public String getStr()
  32. {
  33. if (str == null)
  34. return "str is null";
  35. return str;
  36. }
  37.  
  38. public void setStr(String str)
  39. {
  40. this.str = str;
  41. }
  42. }

我的web.xml的正文:

  1. <display-name>Spring3MVC</display-name>
  2.  
  3. <context-param>
  4. <param-name>contextConfigLocation</param-name>
  5. <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  6. </context-param>
  7.  
  8. <welcome-file-list>
  9. <welcome-file>index.jsp</welcome-file>
  10. </welcome-file-list>
  11.  
  12. <servlet>
  13. <servlet-name>spring</servlet-name>
  14. <servlet-class>
  15. org.springframework.web.servlet.DispatcherServlet
  16. </servlet-class>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>spring</servlet-name>
  21. <url-pattern>*.html</url-pattern>
  22. </servlet-mapping>

解决方法

在每个请求中,您创建一个全新的Spring应用程序上下文,然后在每个操作的新应用程序上下文中获取新对象.所以你应该在web.xml中加载你的spring上下文使用’ContextLoaderListener’.

web.xml中的引用片段

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>
  4. classpath*:spring/appContext.xml classpath*:spring/appContext-security.xml
  5. </param-value>
  6. </context-param>
  7.  
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>

看你的代码

  1. try
  2. {
  3. System.out.println("Accessing Application Context");
  4. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  5. ...

有关Spring上下文加载的更多信息,请参阅MKyong ‘s tutorialSpring reference

猜你在找的Java相关文章