【转】五月的仓颉Java知音
publicinterfaceDao { publicvoidinsert(); publicvoiddelete(); publicvoidupdate(); }
publicclassDaoImplimplementsDao{ @Override publicvoidinsert(){ System.out.println("DaoImpl.insert()"); } @Override publicvoiddelete(){ System.out.println("DaoImpl.delete()"); } @Override publicvoidupdate(){ System.out.println("DaoImpl.update()"); } }
最原始的写法,我要在调用insert()与update()方法前后分别打印时间,就只能定义一个新的类包一层,在调用insert()方法与update()方法前后分别处理一下,新的类我命名为ServiceImpl,其实现为:
publicclassServiceImpl { private Dao dao = new DaoImpl(); publicvoidinsert() { System.out.println("insert()方法开始时间:" + System.currentTimeMillis()); dao.insert(); System.out.println("insert()方法结束时间:" + System.currentTimeMillis()); } publicvoiddelete() { dao.delete(); } publicvoidupdate() { System.out.println("update()方法开始时间:" + System.currentTimeMillis()); dao.update(); System.out.println("update()方法结束时间:" + System.currentTimeMillis()); } }
这是最原始的写法,这种写法的缺点也是一目了然:
publicclassLogDaoimplementsDao{ private Dao dao; publicLogDao(Dao dao){ this.dao = dao; } @Override publicvoidinsert(){ System.out.println("insert()方法开始时间:" + System.currentTimeMillis()); dao.insert(); System.out.println("insert()方法结束时间:" + System.currentTimeMillis()); } @Override publicvoiddelete(){ dao.delete(); } @Override publicvoidupdate(){ System.out.println("update()方法开始时间:" + System.currentTimeMillis()); dao.update(); System.out.println("update()方法结束时间:" + System.currentTimeMillis()); } }
publicclass LogInvocationHandler implements InvocationHandler { privateObject obj; public LogInvocationHandler(Object obj) { this.obj = obj; } @Override publicObject invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); if ("insert".equals(methodName) || "update".equals(methodName)) { System.out.println(methodName + "()方法开始时间:" + System.currentTimeMillis()); Object result = method.invoke(obj, args); System.out.println(methodName + "()方法结束时间:" + System.currentTimeMillis()); return result; } return method.invoke(obj, args); } }
publicstaticvoidmain(String[] args) { Dao dao = new DaoImpl(); Dao proxyDao = (Dao)Proxy.newProxyInstance( LogInvocationHandler.class.getClassLoader(), new Class<?>[]{Dao.class}, new LogInvocationHandler(dao)); proxyDao.insert(); System.out.println("----------分割线----------"); proxyDao.delete(); System.out.println("----------分割线----------"); proxyDao.update(); }
publicclassDaoProxyimplementsMethodInterceptor { @Override public Object intercept(Object object, Method method, Object[] objects, MethodProxy proxy) throws Throwable { String methodName = method.getName(); if ("insert".equals(methodName) || "update".equals(methodName)) { System.out.println(methodName + "()方法开始时间:" + System.currentTimeMillis()); proxy.invokeSuper(object, objects); System.out.println(methodName + "()方法结束时间:" + System.currentTimeMillis()); returnobject; } proxy.invokeSuper(object, objects); returnobject; } }
publicstaticvoidmain(String[] args) { DaoProxy daoProxy = new DaoProxy(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(DaoImpl.class); enhancer.setCallback(daoProxy); Dao dao = (DaoImpl)enhancer.create(); dao.insert(); System.out.println("----------分割线----------"); dao.delete(); System.out.println("----------分割线----------"); dao.update(); }
publicclassTimeHandler { publicvoidprintTime(ProceedingJoinPoint pjp) { Signature signature = pjp.getSignature(); if (signature instanceof MethodSignature) { MethodSignature methodSignature = (MethodSignature)signature; Method method = methodSignature.getMethod(); System.out.println(method.getName() + "()方法开始时间:" + System.currentTimeMillis()); try { pjp.proceed(); System.out.println(method.getName() + "()方法结束时间:" + System.currentTimeMillis()); } catch (Throwable e) { } } } }
<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="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" 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"> <beanid="daoImpl"class="org.xrq.spring.action.aop.DaoImpl" /> <beanid="timeHandler"class="org.xrq.spring.action.aop.TimeHandler" /> <aop:config> <aop:pointcutid="addAllMethod"expression="execution(* org.xrq.spring.action.aop.Dao.*(..))" /> <aop:aspectid="time"ref="timeHandler"> <aop:beforemethod="printTime"pointcut-ref="addAllMethod" /> <aop:aftermethod="printTime"pointcut-ref="addAllMethod" /> </aop:aspect> </aop:config> </beans>
publicclassAopTest { @Test @SuppressWarnings("resource") publicvoidtestAop() { ApplicationContext ac = new ClassPathXmlApplicationContext("spring/aop.xml"); Dao dao = (Dao)ac.getBean("daoImpl"); dao.insert(); System.out.println("----------分割线----------"); dao.delete(); System.out.println("----------分割线----------"); dao.update(); } }
publicclass TransactionHandler { publicvoid commit(JoinPoint jp) { Object obj = jp.getTarget(); if (obj instanceof MailDao) { Signature signature = jp.getSignature(); if (signature instanceof MethodSignature) { SqlSession sqlSession = SqlSessionThrealLocalUtil.getSqlSession(); MethodSignature methodSignature = (MethodSignature)signature; Method method = methodSignature.getMethod(); String methodName = method.getName(); if (methodName.startsWith("insert") || methodName.startsWith("update") || methodName.startsWith("delete")) { sqlSession.commit(); } sqlSession.close(); } } } }
publicclass PermissionHandler { publicvoid hasPermission(JoinPoint jp) throws Exception { Object obj = jp.getTarget(); if (obj instanceof Controller) { Signature signature = jp.getSignature(); MethodSignature methodSignature = (MethodSignature)signature; // 获取方法签名 Method method = methodSignature.getMethod(); // 获取方法参数 Object[] args = jp.getArgs(); // Controller中唯一一个方法的方法签名ModelAndView //handleRequest(HttpServletRequest request, //HttpServletResponse response) throws Exception; // 这里对这个方法做一层判断 if ("handleRequest".equals(method.getName()) && args.length == 2) { Object firstArg = args[0]; if (obj instanceof HttpServletRequest) { HttpServletRequest request = (HttpServletRequest)firstArg; // 获取用户id long userId = Long.parseLong(request.getParameter("userId")); // 获取当前请求路径 String requestUri = request.getRequestURI(); if(!PermissionUtil.hasPermission(userId, requestUri)) { thrownew Exception("没有权限"); } } } } } }
原文:https://www.cnblogs.com/jasonbean/p/10409662.html