Spring -- 使用MethodInterceptor实现AOP

.1. 先写出业务对象接口及实现业务对象。


1.public interface Interface {   
2. public void hello();
3.}
public interface Interface {
public void hello();
}

这里写俩个实现。


1.public class InterfaceImpl implements Interface {   
2.
3. /**
4. * @see com.alipay.aop.BusinessInterface#hello()
5. */
6. @Override
7. public void hello() {
8. System.out.println("AOP TEST!!");
9. }
10.}




1.public class InterfaceImplTest implements Interface {   
2.
3. /**
4. * @see aop.Interface#hello()
5. */
6. @Override
7. public void hello() {
8. System.out.println("helo world!!");
9. }
10.
11.}



2. 实现自己的代理,创建自己的interceptor


1.public class MyInterceptor implements MethodInterceptor {   
2.
3. /**
4. * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
5. */
6. @Override
7. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
8. String info = methodInvocation.getMethod().getDeclaringClass()+ "." +
9. methodInvocation.getMethod().getName() + "()";
10.
11. System.out.println(info);
12.
13. try{
14. Object result = methodInvocation.proceed();
15. return result;
16. }
17. finally{
18. System.out.println(info);
19. }
20. }
21.}


3. 配置文件
<?xml version="1.0" encoding="GBK"?>

1.<beans xmlns="http://www.springframework.org/schema/beans"  
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xmlns:p="http://www.springframework.org/schema/p"
4. xmlns:sofa="http://img.alipay.net/dtd/schema/service"
5. xmlns:context="http://www.springframework.org/schema/context"
6. xmlns:webflow="http://www.springframework.org/schema/webflow-config"
7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8. http://img.alipay.net/dtd/schema/service http://img.alipay.net/dtd/schema/service/sofa-service.xsd
9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10. http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"
11. default-autowire="byName">
12.
13. <bean id="beanTarget" class="aop.InterfaceImpl"/>
14.
15. <bean id="hello" class="aop.InterfaceImplTest" />
16.
17. <bean id="myInterceptor" class="aop.MyInterceptor"/>
18.
19. <bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
20. <property name="proxyInterfaces">
21. <value>aop.Interface</value>
22. </property>
23.
24. <property name="interceptorNames">
25. <list>
26. <value>myInterceptor</value>
27. <value>beanTarget</value>
28. </list>
29. </property>
30. </bean>
31.
32. <bean id="testBean" class="org.springframework.aop.framework.ProxyFactoryBean">
33. <property name="proxyInterfaces">
34. <value>aop.Interface</value>
35. </property>
36.
37. <property name="interceptorNames">
38. <list>
39. <value>myInterceptor</value>
40. <value>hello</value>
41. </list>
42. </property>
43. </bean>
44.</beans>



4. 测试代码
1.public class Main {   
2.
3. /**
4. *
5. * @param args
6. */
7. public static void main(String[] args) {
8. ClassPathResource resource = new ClassPathResource("spring.xml");
9. XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
10. Interface bean = (Interface)beanFactory.getBean("bean");
11. bean.hello();
12.
13. Interface testBean = (Interface)beanFactory.getBean("testBean");
14. testBean.hello();
15. }
16.}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值