spring中設計模式的應用

spring中常用的設計模式達到九種,我們舉例說明:


第一種:簡單工廠

又叫做靜態工廠方法(StaticFactory Method)模式,但不屬於23GOF設計模式之一。 
簡單工廠模式的實質是由一個工廠類根據傳入的參數,動態決定應該創建哪一個產品類。 
spring
中的BeanFactory就是簡單工廠模式的體現,根據傳入一個唯一的標識來獲得bean對象,但是否是在傳入參數後創建還是傳入參數前創建這個要根據具體情況來定。如下配置,就是在 HelloItxxz 類中創建一個 itxxzBean

<beans>
    <bean id="singletonBean" class="com.itxxz.HelloItxxz">
        <constructor-arg>
            <value>Hello! 這是singletonBean!value>
        </constructor-arg>
   </ bean>
    <bean id="itxxzBean" class="com.itxxz.HelloItxxz"
        singleton="false">
        <constructor-arg>
            <value>Hello! 這是itxxzBean! value>
        </constructor-arg>
    </bean>
</beans>


第二種:工廠方法(Factory Method
 

通常由應用程序直接使用new創建新的對象,爲了將對象的創建和使用相分離,採用工廠模式,即應用程序將對象的創建及初始化職責交給工廠對象。

一般情況下,應用程序有自己的工廠對象來創建bean.如果將應用程序自己的工廠對象交給Spring管理,那麼Spring管理的就不是普通的bean,而是工廠Bean

以工廠方法中的靜態方法爲例講解一下:

import java.util.Random;

public class StaticFactoryBean {
      public static Integer createRandom() {
           return new Integer(new Random().nextInt());
       }
}

 

建一個config.xm配置文件,將其納入Spring容器來管理,需要通過factory-method指定靜態方法名稱

<bean id="random" class="example.chapter3.StaticFactoryBean" 
    factory-method="createRandom"/>
<!-- //createRandom方法必須是static的,才能找到 scope="prototype" -->

測試:

//調用getBean()時,返回隨機數.如果沒有指定factory-method,會返回StaticFactoryBean的實例,返回
//工廠Bean的實例
public static void main(String[] args) {      
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));     
    System.out.println("創建的實例:"+factory.getBean("random").toString());
}

第三種:單例模式(Singleton

保證一個類僅有一個實例,並提供一個訪問它的全局訪問點。 
spring
中的單例模式完成了後半句話,即提供了全局的訪問點BeanFactory。但沒有從構造器級別去控制單例,這是因爲spring管理的是是任意的java對象。 
核心提示點:Spring下默認的bean均爲singleton,可以通過singleton=“true|false” 或者 scope=“來指定

第四種:適配器(Adapter

SpringAop中,使用的Advice(通知)來增強被代理類的功能。Spring實現這一AOP功能的原理就使用代理模式(1JDK動態代理。2CGLib字節碼生成技術代理。)對類進行方法級別的切面增強,即,生成被代理類的代理類, 並在代理類的方法前,設置攔截器,通過執行攔截器重的內容增強了代理方法的功能,實現的面向切面編程。

Adapter類接口Target

public interface AdvisorAdapter {
    boolean supportsAdvice(Advice advice);
    MethodInterceptor getInterceptor(Advisor advisor);
}

 MethodBeforeAdviceAdapterAdapter

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {

      public boolean supportsAdvice(Advice advice) {
            return (advice instanceof MethodBeforeAdvice);
      }

      public MethodInterceptor getInterceptor(Advisor advisor) {
            MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
            return new MethodBeforeAdviceInterceptor(advice);
      }
}


第五種:包裝器(Decorator

在我們的項目中遇到這樣一個問題:我們的項目需要連接多個數據庫,而且不同的客戶在每次訪問中根據需要會去訪問不同的數據庫。我們以往在springhibernate框架中總是配置一個數據源,因而sessionFactorydataSource屬性總是指向這個數據源並且恆定不變,所有DAO在使用sessionFactory的時候都是通過這個數據源訪問數據庫。但是現在,由於項目的需要,我們的DAO在訪問sessionFactory的時候都不得不在多個數據源中不斷切換,問題就出現了:如何讓sessionFactory在執行數據持久化的時候,根據客戶的需求能夠動態切換不同的數據源?我們能不能在spring的框架下通過少量修改得到解決?是否有什麼設計模式可以利用呢? 
首先想到在springapplicationContext中配置所有的dataSource。這些dataSource可能是各種不同類型的,比如不同的數據庫:OracleSQL ServerMySQL等,也可能是不同的數據源:比如apache 提供的org.apache.commons.dbcp.BasicDataSourcespring提供的org.springframework.jndi.JndiObjectFactoryBean等。然後sessionFactory根據客戶的每次請求,將dataSource屬性設置成不同的數據源,以到達切換數據源的目的。
spring中用到的包裝器模式在類名上有兩種表現:一種是類名中含有Wrapper,另一種是類名中含有Decorator。基本上都是動態地給一個對象添加一些額外的職責。 

第六種:代理(Proxy

爲其他對象提供一種代理以控制對這個對象的訪問。  從結構上來看和Decorator模式類似,但Proxy是控制,更像是一種對功能的限制,而Decorator是增加職責。 
spring
Proxy模式在aop中有體現,比如JdkDynamicAopProxyCglib2AopProxy 

第七種:觀察者(Observer

定義對象間的一種一對多的依賴關係,當一個對象的狀態發生改變時,所有依賴於它的對象都得到通知並被自動更新。
springObserver模式常用的地方是listener的實現。如ApplicationListener 

第八種:策略(Strategy

定義一系列的算法,把它們一個個封裝起來,並且使它們可相互替換。本模式使得算法可獨立於使用它的客戶而變化。 
spring
中在實例化對象的時候用到Strategy模式
SimpleInstantiationStrategy中有如下代碼說明了策略模式的使用情況: 

public class SimpleInstantiationStrategy implements InstantiationStrategy {
 
    // FactoryMethod的ThreadLocal對象,線程所有的變量
    private static final ThreadLocal<Method> currentlyInvokedFactoryMethod = new ThreadLocal<Method>();
 
    // 返回當前線程所有的FactoryMethod變量值
    public static Method getCurrentlyInvokedFactoryMethod() {
        return currentlyInvokedFactoryMethod.get();
    }
 
    // 第一種實例化方法,實現部分,部分抽象
    @Override
    public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner) {
        // bd對象定義裏,是否包含MethodOverride列表;spring有兩個標籤參數會產生MethodOverrides ,分別是 lookup-method,replaced-method 
        // 沒有MethodOverride對象,可以直接實例化
        if (bd.getMethodOverrides().isEmpty()) {
            // 實例化對象的構造方法
            Constructor<?> constructorToUse;
            // 鎖定對象,使獲得實例化構造方法線程安全
            synchronized (bd.constructorArgumentLock) {
                // 查看bd對象裏是否含有
                constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
                // 沒有就生成
                if (constructorToUse == null) {
                    final Class<?> clazz = bd.getBeanClass();
                    if (clazz.isInterface()) {
                        throw new BeanInstantiationException(clazz, "Specified class is an interface");
                    }
                    try {
                        if (System.getSecurityManager() != null) {
                            constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>() {
                                @Override
                                public Constructor<?> run() throws Exception {
                                    return clazz.getDeclaredConstructor((Class[]) null);
                                }
                            });
                        }
                        else {
                            constructorToUse =    clazz.getDeclaredConstructor((Class[]) null);
                        }
                        // 生成成功後,賦值給bd對象,後面使用
                        bd.resolvedConstructorOrFactoryMethod = constructorToUse;
                    }
                    catch (Exception ex) {
                        throw new BeanInstantiationException(clazz, "No default constructor found", ex);
                    }
                }
            }
            // 反射生成對象
            return BeanUtils.instantiateClass(constructorToUse);
        }
        else {
            // 有MethodOverride對象,需要使用另一種實現方式,之類實現
            return instantiateWithMethodInjection(bd, beanName, owner);
        }
    }
 
    // 第一種實例化方法的抽象部分
    protected Object instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner) {
        throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");
    }
 
    // 第二種實例化方法,實現部分,抽象部分
    @Override
    public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,
            final Constructor<?> ctor, Object... args) {
        // 查看bd對象是否有MethodOverride對象
        // 沒有MethodOverride,則直接實例化對象
        if (bd.getMethodOverrides().isEmpty()) {
            if (System.getSecurityManager() != null) {
                // use own privileged to change accessibility (when security is on)
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        ReflectionUtils.makeAccessible(ctor);
                        return null;
                    }
                });
            }
            // 反射實例化對象
            return BeanUtils.instantiateClass(ctor, args);
        }
        else {
            // 有MethodOverride,之類實現實例化方法
            return instantiateWithMethodInjection(bd, beanName, owner, ctor, args);
        }
    }
 
    // 第二種實例化方法的抽象部分
    protected Object instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner,
            Constructor<?> ctor, Object... args) {
        throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");
    }
 
    // 第三種實例化方法,全部實現
    @Override
    public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,
            Object factoryBean, final Method factoryMethod, Object... args) {
 
        try {
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        ReflectionUtils.makeAccessible(factoryMethod);
                        return null;
                    }
                });
            }
            else {
                ReflectionUtils.makeAccessible(factoryMethod);
            }
 
            // currentlyInvokedFactoryMethod,這塊暫時還沒看到在哪個地方用到了
            // 先取出原有的 Method
            Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get();
            try {
                // 設置當前的Method
                currentlyInvokedFactoryMethod.set(factoryMethod);
                // 使用factoryMethod實例化對象
                return factoryMethod.invoke(factoryBean, args);
            }
            finally {
                // 實例化完成,恢復現場
                if (priorInvokedFactoryMethod != null) {
                    currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod);
                }
                else {
                    currentlyInvokedFactoryMethod.remove();
                }
            }
        }
        catch (IllegalArgumentException ex) {
            throw new BeanInstantiationException(factoryMethod.getReturnType(),
                    "Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +
                    "args: " + StringUtils.arrayToCommaDelimitedString(args), ex);
        }
        catch (IllegalAccessException ex) {
            throw new BeanInstantiationException(factoryMethod.getReturnType(),
                    "Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);
        }
        catch (InvocationTargetException ex) {
            String msg = "Factory method '" + factoryMethod.getName() + "' threw exception";
            if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory &&
                    ((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) {
                msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " +
                        "declaring the factory method as static for independence from its containing instance. " + msg;
            }
            throw new BeanInstantiationException(factoryMethod.getReturnType(), msg, ex.getTargetException());
        }
    }
 
}

 

第九種:模板方法(Template Method

定義一個操作中的算法的骨架,而將一些步驟延遲到子類中。Template Method使得子類可以不改變一個算法的結構即可重定義該算法的某些特定步驟。
Template Method模式一般是需要繼承的。這裏想要探討另一種對Template Method的理解。spring中的JdbcTemplate,在用這個類時並不想去繼承這個類,因爲這個類的方法太多,但是我們還是想用到JdbcTemplate已有的穩定的、公用的數據庫連接,那麼我們怎麼辦呢?我們可以把變化的東西抽出來作爲一個參數傳入JdbcTemplate的方法中。但是變化的東西是一段代碼,而且這段代碼會用到JdbcTemplate中的變量。怎麼辦?那我們就用回調對象吧。在這個回調對象中定義一個操縱JdbcTemplate中變量的方法,我們去實現這個方法,就把變化的東西集中到這裏了。然後我們再傳入這個回調對象到JdbcTemplate,從而完成了調用。這可能是Template Method不需要繼承的另一種實現方式吧。 

以下是一個具體的例子: 
JdbcTemplate
中的execute方法 

@Override
@Nullable
public <T> T execute(ConnectionCallback<T> action) throws DataAccessException {
	Assert.notNull(action, "Callback object must not be null");

	Connection con = DataSourceUtils.getConnection(obtainDataSource());
	try {
		// Create close-suppressing Connection proxy, also preparing returned Statements.
		Connection conToUse = createConnectionProxy(con);
		return action.doInConnection(conToUse);
	}
	catch (SQLException ex) {
		// Release Connection early, to avoid potential connection pool deadlock
		// in the case when the exception translator hasn't been initialized yet.
		String sql = getSql(action);
		DataSourceUtils.releaseConnection(con, getDataSource());
		con = null;
		throw translateException("ConnectionCallback", sql, ex);
	}
	finally {
		DataSourceUtils.releaseConnection(con, getDataSource());
	}
}

 

調用JdbcTemplate的execute方法 

public void testJdbcTemplate(){
	jdbcTemplate.execute(new ConnectionCallback() {

		@Override
		public Object doInConnection(Connection con) throws SQLException, DataAccessException {
			PreparedStatement ps = null;
			try {
				//getInsertString  獲取插入語句的方法
				ps = con.prepareStatement(getInsertString());
				ps.executeUpdate();
			} finally {
				JdbcUtils.closeStatement(ps);
			}
			return null;
		}
	});
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章