最近深入的學習了一下 Spring, 感受到了 AOP 的威力,以前看過 BEA 的 DEV2DEV 雜志,有一期專門的 AOP ,里面很詳細講的,看了有一年多了,今天真正來作個例子
這個例子也是從 dev2dev 上看的,
1 :建一個接口 IBusinessLogic
package test;
public interface IBusinessLogic {
?????? public void foo(String i);
}
2 :建一個接口實現的類
package test;
public class BusinessLogic implements IBusinessLogic {
?????? public void foo(String i) {
????????????? System.out.println("Inside BusinessLogic.foo()");
?????? }
}
3 建立一個應用程序 MainApplication
?????? public static void main(String[] args) {
????????????? ?// Read the configuration file
??????? ApplicationContext ctx =
????????? new FileSystemXmlApplicationContext(
??????????? "E:\\work\\Test\\src\\spring-config.xml");
??????? //Instantiate an object
??????? IBusinessLogic testObject =
????????? (IBusinessLogic) ctx.getBean("businesslogicbean");
??????? testObject.foo("11");
?????? }
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
beans
PUBLIC
?????????
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
??
<!-- Bean configuration -->
??
<
bean
id
=
"businesslogicbean"
??
class
=
"org.springframework.aop.framework.ProxyFactoryBean"
>
?????
<
property
name
=
"proxyInterfaces"
>
????????
<
value
>
test.IBusinessLogic
</
value
>
?????
</
property
>
?????
<
property
name
=
"target"
>
????????
<
ref
local
=
"beanTarget"
/>
?????
</
property
>
???? <property name="interceptorNames">
???????? <list>
??????????? <value>theTracingBeforeAdvisor</value>
??????????? <value>theTracingAfterAdvisor</value>
???????? </list>
???????? </property>
????
??
</
bean
>
??
<!-- Bean Classes -->
??
<
bean
id
=
"beanTarget"
??
class
=
"test.BusinessLogic"
/>
??
????? <bean id="theTracingBeforeAdvisor"
????? class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
????? <property name="advice">
???????? <ref local="theTracingBeforeAdvice"/>
????? </property>
????? <property name="pattern">
???????? <value>.*</value>
????? </property>
?? </bean>
???
?? <!-- Advisor pointcut definition for after advice -->
?? <bean id="theTracingAfterAdvisor"
????? class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
????? <property name="advice">
???????? <ref local="theTracingAfterAdvice"/>
????? </property>
????? <property name="pattern">
???????? <value>.*</value>
????? </property>
?? </bean>
?? <!-- Advice classes -->
?? <bean id="theTracingBeforeAdvice"
????? class="test.TracingBeforeAdvice"/>
?? <bean id="theTracingAfterAdvice"
????? class="test.TracingAfterAdvice"/>
?????
?????
</
beans
>
紅色的暫時不配置,你就可以看到打印出來的結果
Inside BusinessLogic.foo()
4 :建立 2 個類 TracingBeforeAdvice
package test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class TracingBeforeAdvice implements MethodBeforeAdvice {
?????? public void before(Method m, Object[] args, Object target) throws Throwable {
????????????? System.out.println(m.getClass());
????????????? System.out.println(args);
????????????? System.out.println(target);
????????????? System.out.println("Hello world! (by " + this.getClass().getName()
??????????????????????????? + ")");
?????? }
}
TracingAfterAdvice
package test;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class TracingAfterAdvice
implements AfterReturningAdvice
{
?public void afterReturning(Object object,
????????????????????????? Method m,
????????????????????????? Object[] args,
????????????????????????? Object target)
????????????????????????? throws Throwable
?{
???? System.out.println(
?????? "Hello world! (by " +
?????? this.getClass().getName() +
?????? ")");
?}
}
然后把紅色部分的配置上去,
運行 MainApplication
你就可以看到
//class java.lang.reflect.Method
//[Ljava.lang.Object;@
//test.BusinessLogic@
Hello world! (by test.TracingBeforeAdvice)
Inside BusinessLogic.foo()
Hello world! (by test.TracingAfterAdvice)
AOP 在方法開始和技術的時候起到了作用!!!