加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在Spring中池对象?

发布时间:2020-12-14 05:50:58 所属栏目:Java 来源:网络整理
导读:我正在关注如何在 Spring中汇集对象的这个 tutorial.我按照教程中的说明进行操作,但是当我运行我的应用程序时,它总是会生成一个新的对象实例.我期待,因为我正在汇集对象,现有的对象将被重用.因此,不应创建新实例.此外,当我访问bean的getter方法时,会再次创建
我正在关注如何在 Spring中汇集对象的这个 tutorial.我按照教程中的说明进行操作,但是当我运行我的应用程序时,它总是会生成一个新的对象实例.我期待,因为我正在汇集对象,现有的对象将被重用.因此,不应创建新实例.此外,当我访问bean的getter方法时,会再次创建bean的新实例.

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

以下是我的代码:

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

<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","Error! " + e.getMessage());
    }
}

我正在汇集的bean:

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>

解决方法

在每个请求中,您创建一个全新的Spring应用程序上下文,然后在每个操作的新应用程序上下文中获取新对象.所以你应该在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 ‘s tutorial或Spring reference

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读