最近深入的学习了一下
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;@3a9bba
//test.BusinessLogic@1c5ddc9
Hello world! (by test.TracingBeforeAdvice)
Inside BusinessLogic.foo()
Hello world! (by test.TracingAfterAdvice)
AOP
在方法开始和技术的时候起到了作用!!!
posted on 2006-05-18 18:04
小小程序程序员混口饭吃 阅读(396)
评论(0) 编辑 收藏 所属分类:
java