org.studio.crusoe.framework组件包,立足于平时J2SE和J2EE开发中常见运用场景,基于Java核心类库和成熟的第三方工具包,建立方便开发的功能性组件包。
组件包原型为开发过程中的工具类,进行重新的抽象、封装,使之成为跨系统、业务无关的组件。随时更新组件包更能,保证源码和使用示例源码的开发,利用空余时间解决大家使用过程中的问题,及时修复组件包BUG。
实现功能:
V 0.2.0
AdviceClassFramework配置文件示例:
<?xml version="1.0" encoding="UTF-8"?> <class-configuration> <class id="BusinessTestClass" name="org.studio.crusoe.framework.aop.invokecontrol.BusinessTestClass" isSingle="Y"> <attribute name="prp" type="java.lang.String" value="abcs-1"/> <advice> <patterns> <pattern>do*</pattern> </patterns> <intercepts> <intercept adviceClassId="MethodTestAdvice" /> </intercepts> </advice> </class> <adviceClass id="MethodTestAdvice" name="org.studio.crusoe.framework.aop.intercept.MethodTestAdvice" /> <extFile>web/WEB-INF/classes/AdviceTest_2.cfg.xml</extFile> </class-configuration>
API示例:
AdviceClassFramework.load("web/WEB-INF/classes/AdviceTest.cfg.xml"); AdviceClassFramework.init(); BusinessTestClass busi2 = (BusinessTestClass) AdviceClassFramework .getClass("BusinessTestClass2"); busi2.doBusiness();//此方法受到方法增强影响 System.out.println(busi2.getPrp());//根据配置文件中的注入内容输出
V 0.1.4
API接口示例:
ExpressionSession session = new ExpressionSession("\"Hello \" + edition");// edition是变量名,hello是字符串 session.setVariable("edition", "test expression"); System.out.println(session.excute()); session.setVariable("edition", "test expression-2"); //可以为变量设置新的值 System.out.println(session.excute()); ExpressionSession session = new ExpressionSession("(123+12) + edition"); //可以进行数字计算 session.setVariable("edition", 11); //注意变量值类型 System.out.println(session.excute());
V 0.1.3
API接口说明:
/** * 初始化forward模式调度信息 * * @param dispatchId * forward模式调度id * @param forwards * forward模式调度实例列表 * @throws ForwardException * @author wusheng */ public void initDispatch(String dispatchId, List<IForward> forwards) throws ForwardException; /** * 调度执行 * * @param dispatchId * forward模式调度id * @param context * 本次调度执行上下文 * @throws ForwardException * @author wusheng */ public void dispatch(String dispatchId, IContext context) throws ForwardException;
API调用示例:
ForwardDispatch dispatch = new ForwardDispatch(); Forward_1 forward_1 = new Forward_1(); Forward_2 forward_2 = new Forward_2(); List<IForward> list = new ArrayList<IForward>(); list.add(forward_1); list.add(forward_2); dispatch.initDispatch("testId", list); dispatch.dispatch("testId", null);
V 0.1.2
IOC属性设置:
//test为需要进行属性设置的对象实例 PropertyUtil util = new PropertyUtil(test); //设置属性 util.set("prp", "setProp"); //获取属性 System.out.println(util.get("prp"));
BUG:
V 0.1.1
ProxyClassInfo示例:
ProxyClassInfo proxyClassInfo = new ProxyClassInfo("BusinessTestClass", "org.studio.crusoe.framework.aop.invokecontrol.BusinessTestClass", Whether.YES, adviceList, "busi*", "do*");
此代理会增强以busi和do开头的方法。
V 0.1.0
AOP API调用入口:
1.方法强化类的初始化
/** * 初始化方法强化实现类 * 此方法在AOP框架初始化时调用 * * @param adviceId 方法强化类ID * @param adviceName 方法强化类类名 * @author wusheng */ ClassProxyFactory.initAdvice(String adviceId, String adviceName);
2.业务类的初始化
/** * 初始化业务类 * * @param classInfos 业务类描述信息 * @throws Throwable * @author wusheng */ ClassProxyFactory.initProxy(ProxyClassInfo... classInfos);
业务类描述包含如下的信息
/** * 业务类类描述<br> * Title: org.studio.crusoe <br> * Description: <br> * Date: 2010-12-3 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public class ProxyClassInfo { /** * 业务类ID */ private String classId; /* * 业务类类名 */ private String className; /** * 类实例引用 * 只在单例模式下生效 */ private Object classInstance; /** * 是否单例 * 默认为"是" */ private Whether isLazy = Whether.YES; /** * 当前业务类,所需的增强类ID集合 */ private List<String> adviceList; /** * 增强方法匹配描述 * V0.1.0未提供 */ private String[] advicePatterns; /** * 业务类信息;构造函数 * * @param classId * 类ID * @param className * 类名 * @param isLazy * 是否缓存单例 * @param adviceList * 增强器列表 * @param advicePatterns * 增强方法匹配描述(可选参数) */ public ProxyClassInfo(String classId, String className, Whether isLazy, List<String> adviceList, String... advicePatterns); }
3.获取业务类响应的代理类 实例
/** * 获取代理类实例 * * @param classId * @return * @throws Throwable * @author wusheng */ ClassProxyFactory.getProxy(String classId)
增强接口:
1.执行前增强
/** * 方法执行前增强 * Title: org.studio.crusoe <br> * Description: 在增强的方法执行前,会执行before<br> * Date: 2010-12-2 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public interface IMethodBeforeAdvice { public void before(Method method, Object[] args, Object obj) throws Throwable; }
2.执行后增强
/** * 方法执行后增强 * Title: org.studio.crusoe <br> * Description: 在增强的方法执行后,会执行after<br> * Date: 2010-12-2 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public interface IMethodAfterAdvice { public void after(Object returnObj, Method method, Object[] args, Object obj)throws Throwable; }
3.环绕增强
/** * 方法执行增强 * Title: org.studio.crusoe <br> * Description: 可在此接口实现类中控制方法是否执行,执行前后的处理逻辑<br> * Date: 2010-12-2 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public interface IMethodAdvice { public Object invoke(MethodInvocation invocation) throws Throwable; }
4.异常增强
/** * 异常处理方法增强 * Title: org.studio.crusoe <br> * Description: 在增强的方法抛出运行时异常时,会执行afterThrows<br> * Date: 2010-12-2 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public interface IThrowsAdvice { public void afterThrows(Throwable throwable); }
AOP使用示例:
测试增强类实现:
package org.studio.crusoe.framework.aop.intercept; import java.lang.reflect.Method; import org.studio.crusoe.framework.aop.invokecontrol.MethodInvocation; public class MethodTestAdvice implements IMethodAdvice, IMethodBeforeAdvice, IMethodAfterAdvice, IThrowsAdvice { public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("method invoke advice"); //throw new Exception(); return invocation.invoke(); } public void before(Method method, Object[] args, Object obj) throws Throwable { System.out.println("before advice"); } public void after(Object returnObj, Method method, Object[] args, Object obj) throws Throwable { System.out.println("after advice:" + returnObj); } public void afterThrows(Throwable throwable) { System.out.println("exception advice"); } }
注:此处为了方便所以一个类实现了所有的增强接口,仅供测试使用。框架会根据实现的接口,决定对应的增强点。
模拟业务类
package org.studio.crusoe.framework.aop.invokecontrol; /** * 业务测试类 * Title: org.studio.crusoe <br> * Description: <br> * Date: 2010-12-3 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public class BusinessTestClass { /** * 业务测试方法 * @return * @author wusheng */ public String doBusiness(){ System.out.println("start process business"); return "business end"; } }
测试入口
package org.studio.crusoe.framework.aop.invokecontrol; import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.studio.crusoe.framework.aop.constant.Whether; /** * AOP框架逻辑测试 Title: org.studio.crusoe <br> * Description: <br> * Date: 2010-12-3 <br> * Copyright (c) 2010 Crusoe Studio <br> * * @author wusheng */ public class ClassProxyFactoryTester { /** * 测试AOP的类配置和方法强化 * * @throws Throwable * @author wusheng */ @Test public void proxyInvokeTest() throws Throwable { //初始化增强器 ClassProxyFactory.initAdvice("MethodTestAdvice", "org.studio.crusoe.framework.aop.intercept.MethodTestAdvice"); //业务增强类集合 List<String> adviceList = new ArrayList<String>(); adviceList.add("MethodTestAdvice"); //设置初始化业务类描述信息 ProxyClassInfo proxyClassInfo = new ProxyClassInfo("BusinessTestClass", "org.studio.crusoe.framework.aop.invokecontrol.BusinessTestClass", Whether.YES, adviceList); //初始化业务代理类 ClassProxyFactory.initProxy(proxyClassInfo); //获取业务代理类实例 BusinessTestClass busi = (BusinessTestClass)ClassProxyFactory.getProxy("BusinessTestClass"); //业务方法执行,过程中之前设置的方法增强会发生作用 busi.doBusiness(); } }
执行结果:
before advice
method invoke advice
start process business
after advice:business end