此前對於AOP的使用僅限於聲明式事務
對部分函數的調用進行日志記錄
監控部分重要函數
金控部分重要函數的執行時間
事實上
需要打印日志的函數分散在各個包中
類似
該需求通常用於監控某些函數的執行時間
終於下定決心
切面類TestAspect
[java]
package com
/**
* 切面
*
*/
public class TestAspect {
public void doAfter(JoinPoint jp) {
System
+ jp
+ jp
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System
Object retVal = pjp
time = System
System
return retVal;
}
public void doBefore(JoinPoint jp) {
System
+ jp
+ jp
}
public void doThrowing(JoinPoint jp
System
+
System
}
private void sendEx(String ex) {
//TODO 發送短信或郵件提醒
}
}
package com
/**
* 切面
*
*/
public class TestAspect {
public void doAfter(JoinPoint jp) {
System
+ jp
+ jp
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System
Object retVal = pjp
time = System
System
return retVal;
}
public void doBefore(JoinPoint jp) {
System
+ jp
+ jp
}
public void doThrowing(JoinPoint jp
System
+
System
}
private void sendEx(String ex) {
//TODO 發送短信或郵件提醒
}
}
[java]
package com
/**
* 接口A
*/
public interface AService {
public void fooA(String _msg)
public void barA()
}
package com
/**
* 接口A
*/
public interface AService {
public void fooA(String _msg)
public void barA()
} [java]
package com
/**
*接口A的實現類
*/
public class AServiceImpl implements AService {
public void barA() {
System
}
public void fooA(String _msg) {
System
}
}
package com
/**
*接口A的實現類
*/
public class AServiceImpl implements AService {
public void barA() {
System
}
public void fooA(String _msg) {
System
}
}
[java]
package com
/**
* Service類B
*/
public class BServiceImpl {
public void barB(String _msg
System
if(_type ==
throw new IllegalArgumentException(
}
public void fooB() {
System
}
}
package com
/**
* Service類B
*/
public class BServiceImpl {
public void barB(String _msg
System
if(_type ==
throw new IllegalArgumentException(
}
public void fooB() {
System
}
}
ApplicationContext
[java]
<?xml version=
<beans xmlns=
xmlns:xsi=
xmlns:aop=
xsi:schemaLocation=
default
<aop:config>
<aop:aspect id=
<!
<aop:pointcut id=
expression=
<aop:before pointcut
<aop:after pointcut
<aop:around pointcut
<aop:after
</aop:aspect>
</aop:config>
<bean id=
<bean id=
<bean id=
</beans>
<?xml version=
<beans xmlns=
xmlns:xsi=
xmlns:aop=
xsi:schemaLocation=
default
<aop:config>
<aop:aspect id=
<!
<aop:pointcut id=
expression=
<aop:before pointcut
<aop:after pointcut
<aop:around pointcut
<aop:after
</aop:aspect>
</aop:config>
<bean id=
<bean id=
<bean id=
</beans>
測試類AOPTest
[java]
public class AOPTest extends AbstractDependencyInjectionSpringContextTests {
private AService aService;
private BServiceImpl bService;
protected String[] getConfigLocations() {
String[] configs = new String[] {
return configs;
}
/**
* 測試正常調用
*/
public void testCall()
{
System
aService
aService
bService
bService
}
/**
* 測試After
*/
public void testThrow()
{
try {
bService
} catch (IllegalArgumentException e) {
}
}
public void setAService(AService service) {
aService = service;
}
public void setBService(BServiceImpl service) {
bService = service;
}
}
public class AOPTest extends AbstractDependencyInjectionSpringContextTests {
private AService aService;
private BServiceImpl bService;
protected String[] getConfigLocations() {
String[] configs = new String[] {
return configs;
}
/**
* 測試正常調用
*/
public void testCall()
{
System
aService
aService
bService
bService
}
/**
* 測試After
*/
public void testThrow()
{
try {
bService
} catch (IllegalArgumentException e) {
}
}
public void setAService(AService service) {
aService = service;
}
public void setBService(BServiceImpl service) {
bService = service;
}
}
運行結果如下
[java]
log Begining method: com
AServiceImpl
log Ending method: com
process time:
log Begining method: com
AServiceImpl
log Ending method: com
process time:
log Begining method: com
BServiceImpl
log Ending method: com
process time:
log Begining method: com
BServiceImpl
log Ending method: com
process time:
log Begining method: com
BServiceImpl
log Ending method: com
method com
測試異常
log Begining method: com
AServiceImpl
log Ending method: com
process time:
log Begining method: com
AServiceImpl
log Ending method: com
process time:
log Begining method: com
BServiceImpl
log Ending method: com
process time:
log Begining method: com
BServiceImpl
log Ending method: com
process time:
log Begining method: com
BServiceImpl
log Ending method: com
method com
測試異常
《Spring參考手冊》中定義了以下幾個AOP的重要概念
切面(Aspect)
連接點(Joinpoint)
通知(Advice)
切入點(Pointcut)
目標對象(Target Object)
AOP代理(AOP Proxy)在Spring AOP中有兩種代理方式
通知(Advice)類型
前置通知(Before advice)
後通知(After advice)
返回後通知(After return advice)
環繞通知(Around advice)
拋出異常後通知(After throwing advice)
切入點表達式
通常情況下
[java]
execution(modifiers
execution(modifiers
ret
declaring
name
parm
throws
其中
通知參數
可以通過args來綁定參數
[java]
<aop:config>
<aop:aspect id=
<aop:pointcut id=
expression=
<aop:after pointcut
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect id=
<aop:pointcut id=
expression=
<aop:after pointcut
</aop:aspect>
</aop:config>TestAspect的doAfter方法中就可以訪問msg參數
[java]
public void doAfter(JoinPoint jp
public void doAfter(JoinPoint jp
任何通知(Advice)方法可以將第一個參數定義為 org
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28867.html