您的位置:首页 > 编程语言 > Java开发

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

2017-07-26 16:15 429 查看
通知可理解为增强代码

前置通知 org.springframework.aop.MethodBeforeAdvice

后置通知 org.springframework.aop.AfterReturningAdvice

环绕通知 org.aopalliance.intercept.MethodInterceptor

异常抛出通知 org.springframework.aop.ThrowsAdvice

  

导入Jar包:

   1. spring-aop-3.2.0.RELEASE.jar

   2.com.springsource.org.aopalliance-1.0.0.jar

编写代理对象:

Customer接口:

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 stub
System.out.println("添加用户......");
}

public void update() {
// TODO Auto-generated method stub
System.out.println("修改用户......");
}

public void delete() {
// TODO Auto-generated method stub
System.out.println("删除用户......");
}

public void find() {
// TODO Auto-generated method stub
System.out.println("查询用户......");
}

}
MyBeforeAdvice前置增强:

package cn.nedu.wy.demo03;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

//前置增强
public class MyBeforeAdvice implements MethodBeforeAdvice {
// method 代表执行的方法 args代表方法参数 target代表目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("前置增强......");
}

}


配置文件:
<bean id="customerDao" class="cn.nedu.wy.demo03.CustomerDaoImpl"></bean>
<!--定义增强 -->
<bean id="beforeAdvice" class="cn.nedu.wy.demo03.MyBeforeAdvice"></bean>
<!-- Spring支持配置生成代理 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerDao"></property>
<property name="proxyInterfaces" value="cn.nedu.wy.demo03.CustomerDao"></property>
<property name="interceptorNames" value="beforeAdvice"></property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: