title: Spring AOP
date: ‘2018-05-29 15:59:26’
updated: ‘2018-05-30 00:49:26’
tags: [Spring, AOP]
permalink: /articles/2018/05/29/1527580766021.html
有关AOP的三个名词
| 名称 | 中文 | 解释 |
| — | — | — | — |
| PointCut | 切入点 | 程序运行的某个阶段点,如某个方法调用 |
| Joint Point | 连接点 | 连接点的集合是切入点 |
| Adivice |通知|如输出日志的代码.|
| Advisor |配置器|将通知注入切入点|
Spring AOP实现输出日志
采用Interceptor Around通知
接口
TimeBookInerface
1 2 3 4 5 6 7 8 9 10
| package xyz.log.aop.impl;
public interface TimeBookInterface {
public void doAuditing(String name); }
|
FinanceInterface
1 2 3 4 5 6
| package xyz.log.aop.impl;
public interface FinanceInterface { public void doCheck(String name); }
|
impl
1 2 3 4 5 6 7 8 9 10 11
| package xyz.log.aop.action;
import xyz.log.aop.impl.TimeBookInterface;
public class TimeBook implements TimeBookInterface { @Override public void doAuditing(String name) { } }
|
1 2 3 4 5 6 7 8 9 10
| package xyz.log.aop.action;
import xyz.log.aop.impl.FinanceInterface;
public class Finance implements FinanceInterface { @Override public void doCheck(String name) {
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package xyz.log.aop.log;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.log4j.Level; import org.apache.log4j.Logger;
public class LogAround implements MethodInterceptor { private Logger logger = Logger.getLogger(this.getClass().getName());
@Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { logger.log(Level.INFO, methodInvocation.getArguments()[0] + "开始审核数据"); try { Object result = methodInvocation.proceed(); return result;
} finally { logger.log(Level.INFO, methodInvocation.getArguments()[0] + "审核数据结束"); }
} }
``` xml配置
``` xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--使用Spring aop 实现日志输出的Bean--> <!--注册日志bean组件--> <bean id="log" class="xyz.log.aop.log.LogAround"/> <!--审核程序--> <bean id="timeBook" class="xyz.log.aop.action.TimeBook"/> <!--使用Spring ProxyFactoryBean来实现代理--> <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces" > <value>xyz.log.aop.impl.TimeBookInterface</value> </property> <property name="target" > <ref bean="timeBook"/> </property> <!--指定要代理的类--> <property name="interceptorNames" > <list> <value>log</value> </list> </property> </bean> <!--财务审核--> <bean id="finance" class="xyz.log.aop.action.Finance"/> <bean id="logProxy1" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces" > <value>xyz.log.aop.impl.FinanceInterface</value> </property> <property name="target" > <ref bean="finance"/> </property> <!--指定要代理的类--> <property name="interceptorNames" > <list> <value>log</value> </list> </property> </bean>
</beans>
|
test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package xyz.log.aop.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import xyz.log.aop.impl.FinanceInterface; import xyz.log.aop.impl.TimeBookInterface;
public class TestHelloWorld { public static void main(String[] args) { ApplicationContext actx = new ClassPathXmlApplicationContext("classpath:SpringConfig.xml"); TimeBookInterface timeBookProxy = (TimeBookInterface) actx.getBean("logProxy"); FinanceInterface financeProxy = (FinanceInterface) actx.getBean("logProxy1"); timeBookProxy.doAuditing("张三"); financeProxy.doCheck("李四"); } }
|