Spring的AOP传统开发方法带切点的切面

来源:互联网 发布:js设置checkbox不可用 编辑:程序博客网 时间:2024/06/07 00:48

AOP传统开发方法

带切点的切面

CustomerDao:

package cn.nedu.wy.demo03;public interface CustomerDao {public void add();public void update();public void delete();public void find();}

CustomerDaoImpl:

package cn.nedu.wy.demo03;public class CustomerDaoImpl implements CustomerDao {public void add() {// TODO Auto-generated method stubSystem.out.println("添加用户......");}public void update() {// TODO Auto-generated method stubSystem.out.println("修改用户......");}public void delete() {// TODO Auto-generated method stubSystem.out.println("删除用户......");}public void find() {// TODO Auto-generated method stubSystem.out.println("查询用户......");}}

环绕增强MyAroundAdvice:

package cn.nedu.wy.demo03;import java.lang.reflect.Method;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.springframework.cglib.proxy.MethodProxy;public class MyAroundAdvice implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation arg0) throws Throwable {// TODO Auto-generated method stubSystem.out.println("环绕前增强......");Object result = arg0.proceed();System.out.println("环绕后增强......");return result;}}



配置文件:

<bean id="customerDao" class="cn.nedu.wy.demo03.CustomerDaoImpl"></bean><!-- 定义增强 --><bean id="aroundAdvice" class="cn.nedu.wy.demo03.MyAroundAdvice"></bean><!-- 定义切点切面: --><bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!-- 定义表达式,规定哪些方法执行拦截,拦截add和find --><property name="patterns" value=".*add.*,.*find.*"></property><!-- 应用增强 --><property name="advice" ref="aroundAdvice"/></bean><!-- 定义生成代理对象 --><bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 配置目标 --><property name="target" ref="customerDao"></property><!-- 针对类的代理 --><property name="proxyTargetClass" value="true"></property><!-- 在目标上应用增强 --><property name="interceptorNames" value="myPointcutAdvisor"></property></bean>


原创粉丝点击