TaskFactory单例模式利用xml
/** * * Copyright (c) 1995-2009 Wonders Information Co.,Ltd. * 1518 Lianhang Rd,Shanghai 201112.P.R.C. * All Rights Reserved. * * This software is the confidential and proprietary information of Wonders Group. * (Social Security Department). You shall not disclose such * Confidential Information and shall use it only in accordance with * the terms of the license agreement you entered into with Wonders Group. * * Distributable under GNU LGPL license by gnu.org */ package com.yundaex.common.parent.threadpool; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Repository; /** * <p> * Title: Adage_[子系统统名]_[模块名] * </p> * <p> * Description: 并发任务工厂,用于动态获取并发任务实例 * 注意:主要用于事务包装的并发任务,在用SPRING包装任务的事务时,需要注意,任务bean和任务代理bean必须均为Prototype类型(singleton为false) * </p> * * @author Jessy * @version $Revision$ 2010-8-4 * @author (lastest modification by $Author$) * @since 1.0 */ @Repository("taskFactory") public final class TaskFactory implements ApplicationContextAware { private ApplicationContext applicationContext; private static TaskFactory instance; private TaskFactory() { } /** * <p> * Discription:获取任务工厂实例 * </p> * * @return */ public static TaskFactory getInstance() { if (instance == null) { instance = new TaskFactory(); } return instance; } /** * <p> * Discription:创建并发任务 * </p> * * @param <T> 泛型 * @param type 任务类型 * @param taskClass 任务CLASS * @return */ public <T extends Task> T createTask(String type, Class<T> taskClass) { return taskClass.cast(applicationContext.getBean(type)); } /** * {@inheritDoc} * * @see org.springframework.context.ApplicationContextAware# * setApplicationContext(org.springframework.context.ApplicationContext) */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
applicationContext-common-threadpool.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <description></description> <bean id="simpleThreadPool" class="com.yundaex.common.parent.threadpool.runtime.SimpleThreadPool" abstract="true"> </bean> <bean id="dynamicThreadPool" class="com.yundaex.common.parent.threadpool.runtime.DynamicThreadPool" abstract="true"> <!--<property name="maxSize" value="${your.module.maxSize}"></property> <property name="minSize" value="${your.module.minSize}"></property> <property name="checkPeriod" value="${your.module.checkPeriod}"></property> <property name="sparePeriod" value="${your.module.sparePeriod}"></property>--> </bean> <bean id="taskFactory" class="com.yundaex.common.parent.threadpool.TaskFactory" factory-method="getInstance"> </bean> </beans>