如何集中在春季的对象?(How to pool objects in Spring?)

2019-08-17 17:25发布

我下面这个教程就如何集中在春季的对象。 我跟着写在教程的指令,但是当我跑我的应用程序,它总是生成对象的新实例。 我很期待,因为我正在汇集的对象,现有的对象将会被重用。 因此,应创建新的实例。 此外,当我访问bean的getter方法中,再次创建bean的新实例。

什么可能是我做错了什么? 难道我误解春池的概念?

下面是我的代码:

应用环境:(这是我的应用程序上下文的只是身体。)

<bean id="simpleBeanTarget" class="com.bean.SimpleBean" scope="prototype">

</bean>

<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
    <property name="targetBeanName" value="simpleBeanTarget" />
    <property name="maxSize" value="2" />
</bean>

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

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

@RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response)
{
    String message = "Hello World, Spring 3.";
    try
    {
        System.out.println("Accessing Application Context");
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("Getting Bean");
        SimpleBean simpleBean = (SimpleBean) context.getBean("simpleBean");
        //A new SimpleBean... is printed here.

        System.out.println("Displaying Hello World: " + simpleBean.getRandomNum());
        //After this line, A new SimpleBean... is printed again. I simply access the getter method. Why does it create a new instance of SimpleBean?

        return new ModelAndView("hello", "message", message);
    }catch(Exception e)
    {
        System.out.println("Error: " + e);
        e.printStackTrace();
        return new ModelAndView("hello", "message", "Error! " + e.getMessage());
    }
}

我汇集豆:

package com.bean;

import java.util.Random;

public class SimpleBean
{
    int randomNum;
    String str;

    SimpleBean()
    {
        Random randomGenerator = new Random();
        randomNum = randomGenerator.nextInt(100);

        //I'm printing this line just to check if a instance of this bean is created.
        System.out.println("#####################A new SimpleBean was born: " + randomNum);

        str = "This is a string.";
    }

    public int getRandomNum()
    {
        return randomNum;
    }

    public void setRandomNum(int randomNum)
    {
        this.randomNum = randomNum;
    }

    public String getStr()
    {
        if (str == null)
            return "str is null";
        return str;
    }

    public void setStr(String str)
    {
        this.str = str;
    }
}

我的web.xml的身体:

<display-name>Spring3MVC</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Answer 1:

对每个请求,您可以创建一个全新的Spring应用程序上下文,那么你会得到一个新的applicatin上下文中的每个动作的新对象。 所以你应该加载在web.xml中的Spring上下文中使用“ContextLoaderListener的”。

在web.xml中引用片段

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:spring/appContext.xml  classpath*:spring/appContext-security.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

看你的代码:

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

有关Spring上下文加载更多的知识,请参阅MKyong的教程或Spring参考



Answer 2:

当你提到它的意思你是说以服务URS池要创造最大的使用对象状态的两个对象通过URS构造函数中提供的供应商。

  SimpleBean()
    {
        Random randomGenerator = new Random();
        randomNum = randomGenerator.nextInt(100);

        //I'm printing this line just to check if a instance of this bean is created.
        System.out.println("#####################A new SimpleBean was born: " + randomNum);

        str = "This is a string.";

    }

所以为SimpleBean为SimpleBean =(为SimpleBean)context.getBean( “为SimpleBean”); 是汇集对象。 如果两个执行路径想从为SimpleBean服务则该对象实例通过Pooling.If提供了更多的thean两个执行路径则是通过原型范围提供。



Answer 3:

试试这将工作...

<property name="targetSource" ref="poolTargetSource" />

<!-- Added so that different instance of object is created -->
<property name="singleton" value="false" />



文章来源: How to pool objects in Spring?