aop编程 环绕round

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
 5 
 6 <!--     配置代理关系               -->
 7 <!-- 目标类      被代理人对象 -->
 8 <bean id="target" class="dao.imp.UserDaoImp"></bean>
 9 <!-- 切面  方面代码(通知代码)-->
10 <bean id="roundAdvice" class="aop.RoundAdvice"></bean>
11 
12 <!-- 代理类   代理人对象-->
13 <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
14 
15 
16 <!-- 代理用到的接口     拦截者:指明方法代码的封装对象-->
17 <property name="proxyInterfaces">
18 <list>
19 <value>dao.UserDao</value>
20 </list>
21 </property>
22 
23 <!-- 目标对象   指明被代理人-->
24 <property name="target" ref="target">
25 </property>
26 
27 
28 <!-- 切面 拦截者  :指明方面代码的封装对象 -->
29 <property name="interceptorNames">
30 <list>
31 <!-- <value>beforeAdvice</value> -->
32 <value>pointcutAndAdvice</value>
33 </list>
34 </property>
35 </bean>
36 <!-- 配置带切入点的bean 
37 进一步封装代码 ,提供过滤被代理方法的功能 。效果  :被代理接口中方法集合的子集被代理执行--> 
38 <bean id="pointcutAndAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
39     <property name="advice" ref="roundAdvice"></property>
40     <property name="patterns">
41         <list>
42             <value>dao.UserDao.saveUser</value>
43             <value>dao.UserDao.deleteUser</value>
44         </list>
45     </property>
46 </bean>
47 </beans>

applicationContext-round.xml

 

package aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;



public class RoundAdvice implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation method) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("开始");
		Object obj=method.proceed();
		System.out.println("结束");
		
		return null;
	}





}

 RoundAdvice.java

 

 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 
 4 import dao.UserDao;
 5 
 6 
 7 public class TestAop {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext-aop.xml");
12         //获取文件
13         UserDao dao=(UserDao) ctx.getBean("proxy");
14         //得到代理
15         dao.saveUser();
16         dao.deleteUser();
17         dao.findUser();
18     }
19 
20 }

test.java

posted @ 2014-10-27 14:17  马赛克都挡不住  阅读(695)  评论(0编辑  收藏  举报