前面這是講了許多的概念,下面以一個例子來說明:
一個很好理解的aop 例子,也就是日志服務(wù)。
先從aop第一種方式來實現(xiàn),也就是xml配置方式
先創(chuàng)建基本的日志類:
public class Logger {
??? private static Log log = LogFactory.getLog(Logger.class);
??? public void entry(String message) {
??????? log.info(message);
??? }
}
這里只是簡單的一個方法,當(dāng)然實際情況可能不同。
由于xml配置需要一個方面的實現(xiàn)bean
所以創(chuàng)建一個簡單的bean :
public class LogBean {
??? private Logger logger = new Logger();
??? public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable {
??????? logger.entry("before invoke method:"
???????????????????? + joinPoint.getSignature().getName());
??????? Object object = joinPoint.proceed();
??????? logger.entry("after invoke method:"
???????????????????? + joinPoint.getSignature().getName());
??????? return object;
??? }
}
這里采取簡單的around advice,其他類型的advice 基本上都差不多
當(dāng)然有了這兩個核心的日志類,需要一個測試類,用于測試。
public class TestBean {
??? public void method1() {
??????? System.out.println("in method1");
??? }
??? public void method2() {
??????? System.out.println("in method2");
??? }
}
這就是需要測試的類了,需要記錄日志的方法只有兩個,這里用System.out.println,只是想顯示方法的調(diào)用順序。
然后關(guān)鍵的在于xml的配置了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="?xmlns:xsi="?xmlns:aop="http://www.springframework.org/schema/aop"
?xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
?
?<aop:config>
????<!--??expression 表示要執(zhí)行的匹配表達式,這里匹配所有的public方法,但是去除logger類的所有方法,防止無限調(diào)用-->
??????<aop:pointcut id="loggableCalls"
????????? expression="execution(public * *(..)) and !execution(* org.spring.test.aop.log.Logger.*(..))"/>
??<aop:aspect id="logAspect" ref="logBean">
???<aop:around pointcut-ref="loggableCalls" method="aroundLogCalls"/>
??</aop:aspect>
?
?</aop:config>
?<bean id="logBean" class="org.spring.test.aop.log.LogBean" />
?<bean id="testBean" class="org.spring.test.aop.log.TestBean"/>
?
</beans>
現(xiàn)在寫一個測試類:
public class LogXmlTest extends RootTest {
??? @Override
??? protected String getBeanXml() {
??????? return "org/spring/test/aop/log/bean.xml";
??? }
??? public void testLog() {
??????? TestBean bean = (TestBean) ctx.getBean("testBean");
??????? bean.method1();
??????? bean.method2();
??? }
}
public abstract class RootTest extends TestCase {
??? protected ApplicationContext? ctx;
??? protected Log log = LogFactory.getLog(getClass());
??? protected RootTest() {
??????? ctx = new ClassPathXmlApplicationContext(getBeanXml());
??? }
??? protected abstract String getBeanXml();
}
打印的消息如下:
2006-09-17 11:08:28,203 INFO [org.spring.test.aop.log.Logger] - before invoke method:method1
in method1
2006-09-17 11:08:28,203 INFO [org.spring.test.aop.log.Logger] - after invoke method:method1
2006-09-17 11:08:28,218 INFO [org.spring.test.aop.log.Logger] - before invoke method:method2
in method2
2006-09-17 11:08:28,218 INFO [org.spring.test.aop.log.Logger] - after invoke method:method2
第二種實現(xiàn)方式,采用注釋方式:
Logger 類不變
創(chuàng)建一個LogAspect類
@Aspect
public class LogAspect {
??? private Logger logger = new Logger();
??? @Pointcut("execution(public * *(..))")
??? public void publicMethods() {
??? }
??? @Pointcut("execution(* org.spring.test.aop.log.Logger.*(..))")
??? public void logObjectCalls() {
??? }
??? @Pointcut("publicMethods()&&!logObjectCalls()")
??? public void loggableCalls() {
??? }
??? @Around("loggableCalls()")
??? public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable {
??????? logger.entry("before invoke method:"
???????????????????? + joinPoint.getSignature().getName());
??????? Object object = joinPoint.proceed();
??????? logger.entry("after invoke method:"
???????????????????? + joinPoint.getSignature().getName());
??????? return object;
??? }
}
配置文件就簡單多了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="?xmlns:xsi="?xmlns:aop="?xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop?
?<aop:aspectj-autoproxy/>
?
?<!-- 或者使用以下定義
?
?
?
?<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
?
?-->
?<bean id="logAspect" class="org.spring.test.aop.log.LogAspect"/>
?<bean id="testBean" class="org.spring.test.aop.log.TestBean"/>
?
</beans>
測試類:
跟上面的差不多
把xml文件換掉就行
打印的方式差不多
個人還是比較喜歡第二種實現(xiàn)。
posted on 2006-09-17 11:13
布衣郎 閱讀(2825)
評論(4) 編輯 收藏 所屬分類:
aop 、
spring