package
AdvisorTest;
public
interface
Shopping
...
{
public
String buySomething(String type);
public
String buyAnything(String type);
public
void
testException();
}
下面是业务实现类,我们的通知就是以这些实现类作为切面,在业务方法前后加入我们的通知代码
package
AdvisorTest;
public
class
ShoppingImpl
implements
Shopping
...
{
private
Customer customer;
public
Customer getCustomer()
...
{
return
customer;
}
public
void
setCustomer(Customer customer)
...
{
this
.customer
=
customer;
}
public
String buySomething(String type)
...
{
System.out.println(
this
.getCustomer().getName()
+
"
bye
"
+
type
+
"
success
"
);
return
null
;
}
public
String buyAnything(String type)
...
{
System.out.println(
this
.getCustomer().getName()
+
"
bye
"
+
type
+
"
success
"
);
return
null
;
}
public
void
testException()
...
{
throw
new
ClassCastException();
}
}
(1)前置通知
配置了前置通知的bean,在执行业务方法前,均会执行前置拦截器的before方法
package
AdvisorTest;
import
java.lang.reflect.Method;
import
org.springframework.aop.MethodBeforeAdvice;
//
前置通知
public
class
WelcomeAdvice
implements
MethodBeforeAdvice
...
{
public
void
before(Method method, Object[] args, Object obj)
throws
Throwable
...
{
String type
=
(String)args[
0
];
System.out.println(
"
Hello welcome to bye
"
+
type);
}
}
(2)后置通知
配置了前置通知的bean,在执行业务方法前,均会执行前置拦截器的afterReturnning方法
package
AdvisorTest;
import
java.lang.reflect.Method;
import
org.springframework.aop.AfterReturningAdvice;
import
org.springframework.aop.MethodBeforeAdvice;
//
后置通知
public
class
ThankYouAdvice
implements
AfterReturningAdvice
...
{
public
void
afterReturning(Object obj, Method method, Object[] arg1,
Object arg2)
throws
Throwable
...
{
String type
=
(String)arg1[
0
];
System.out.println(
"
Hello Thankyou to bye
"
+
type);
}
}
(3)环绕通知
配置了前置通知的bean,在执行业务方法前后,均会执行前置拦截器的invoke方法
需要注意的是必须调用目标方法,如不调用,目标方法将不被执行
package
AdvisorTest;
import
org.aopalliance.intercept.MethodInterceptor;
import
org.aopalliance.intercept.MethodInvocation;
public
class
MethodAdvisor
implements
MethodInterceptor
...
{
public
Object invoke(MethodInvocation invocation)
throws
Throwable
...
{
String str
=
(String)invocation.getArguments()[
0
];
System.out.println(
"
this is before
"
+
str
+
"
in MethodInterceptor
"
);
Object obj
=
invocation.proceed();
//
调用目标方法,如不调用,目标方法将不被执行
System.out.println(
"
this is after
"
+
str
+
"
in MethodInterceptor
"
);
return
null
;
}
}
public void afterThrowing(Method method,Object[] args,Object target,Throwable throwable){
package
AdvisorTest;
import
org.springframework.aop.ThrowsAdvice;
public
class
ExceptionAdvisor
implements
ThrowsAdvice
...
{
public
void
afterThrowing(ClassCastException e)
...
{
System.out.println(
"
this is from exceptionAdvisor
"
);
}
}
配置文件
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
bean
id
="customer"
class
="AdvisorTest.Customer"
>
<
constructor-arg
index
="0"
>
<
value
>
gaoxiang
</
value
>
</
constructor-arg
>
<
constructor-arg
index
="1"
>
<
value
>
26
</
value
>
</
constructor-arg
>
</
bean
>
<
bean
id
="shoppingImpl"
class
="AdvisorTest.ShoppingImpl"
>
<
property
name
="customer"
>
<
ref
local
="customer"
/>
</
property
>
</
bean
>
<!--
前置通知
-->
<
bean
id
="welcomeAdvice"
class
="AdvisorTest.WelcomeAdvice"
/>
<
bean
id
="welcomeAdviceShop"
class
="org.springframework.aop.framework.ProxyFactoryBean"
>
<
property
name
="proxyInterfaces"
>
<
value
>
AdvisorTest.Shopping
</
value
>
</
property
>
<
property
name
="target"
>
<
ref
local
="shoppingImpl"
/>
</
property
>
<
property
name
="interceptorNames"
>
<
list
>
<
value
>
welcomeAdvice
</
value
>
</
list
>
</
property
>
</
bean
>
<!--
后置通知
-->
<
bean
id
="thankyouAdvice"
class
="AdvisorTest.ThankYouAdvice"
/>
<
bean
id
="thankyouAdviceShop"
class
="org.springframework.aop.framework.ProxyFactoryBean"
>
<
property
name
="proxyInterfaces"
>
<
value
>
AdvisorTest.Shopping
</
value
>
</
property
>
<
property
name
="target"
>
<
ref
local
="shoppingImpl"
/>
</
property
>
<
property
name
="interceptorNames"
>
<
list
>
<
value
>
thankyouAdvice
</
value
>
</
list
>
</
property
>
</
bean
>
<!--
环绕通知
-->
<
bean
id
="methodAdvice"
class
="AdvisorTest.MethodAdvisor"
/>
<
bean
id
="methodAdviceShop"
class
="org.springframework.aop.framework.ProxyFactoryBean"
>
<
property
name
="proxyInterfaces"
>
<
value
>
AdvisorTest.Shopping
</
value
>
</
property
>
<
property
name
="target"
>
<
ref
local
="shoppingImpl"
/>
</
property
>
<
property
name
="interceptorNames"
>
<
list
>
&nb