SSH系列教材 (三)- 单例

一旦Struts的 Action交由Spring进行管理之后,默认情况下,Action是单例的。 这会带来一系列的问题。

步骤1:先运行,看到效果,再学习
步骤2:模仿和排错
步骤3:单例的Action
步骤4:非单例模式

步骤 1 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

步骤 2 : 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

步骤 3 : 单例的Action

在list中加一句话,打印当前Action对象

System.out.println(this);


然后不停刷新

http://127.0.0.1:8080/ssh/listProduct


就可以看到,每次打印的对象,都是同一个对象,这表明Spring管理的bean是单例模式的。

单例的Action

package com.how2java.action;

import java.util.List;

import com.how2java.dao.ProductDAO;

import com.how2java.pojo.Product;

public class ProductAction {

    ProductDAO pdao;

    List<Product> products;

     

    public String list() {

        System.out.println(this);

        products = pdao.find("from Product p");

        return "listJsp";

    }

     

    public ProductDAO getPdao() {

        return pdao;

    }

    public void setPdao(ProductDAO pdao) {

        this.pdao = pdao;

    }

    public List<Product> getProducts() {

        return products;

    }

    public void setProducts(List<Product> products) {

        this.products = products;

    }

}

步骤 4 : 非单例模式

为productActionBean增加一个属性scope="prototype" ,用于告知Spring,这个对象的管理,使用非单例模式
重启tomcat,不停的访问

http://127.0.0.1:8080/ssh/listProduct


可以观察到,每次访问都是不同的ProductAction对象了。

非单例模式

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

   http://www.springframework.org/schema/beans 

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

   http://www.springframework.org/schema/aop 

   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

   http://www.springframework.org/schema/tx 

   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/context      

   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  

    <bean name="productActionBean" scope="prototype" class="com.how2java.action.ProductAction">

        <property name="pdao" ref="pdao" />

    </bean>

     

    <bean name="pdao" class="com.how2java.dao.ProductDAO">

        <property name="sessionFactory" ref="sf" />

    </bean>

    <bean name="sf"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource" ref="ds" />

        <property name="mappingResources">

            <list>

                <value>com/how2java/pojo/Product.hbm.xml</value>

            </list>

        </property>

        <property name="hibernateProperties">

            <value>

                hibernate.dialect=org.hibernate.dialect.MySQLDialect

                hibernate.show_sql=false

                hbm2ddl.auto=update

            </value>

        </property>

    </bean>    

         

    <bean name="ds"

        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>

        <property name="username" value="root" />

        <property name="password" value="admin" />

    </bean>       

</beans>


更多内容,点击了解: https://how2j.cn/k/ssh/ssh-singleton/787.html

原文地址:https://www.cnblogs.com/Lanht/p/12789351.html