代码准备工作:
创建两个dao类文件,一个是无接口customerDao,一个是有接口的StudentDao ,并且编写前置和环绕代码。
基本源码
创建customerDao的类
package com.imooc.aop.demo5;
public class CustomerDao {
public void find(){
System.out.println("查询客户...");
}
public void save(){
System.out.println("保存客户...");
}
public void update(){
System.out.println("修改客户...");
}
public void delete(){
System.out.println("删除客户...");
}
}
创建一个学生的接口和实现类
接口代码
package com.imooc.aop.demo5;
public interface StudentDao {
public void find();
public void save();
public void update();
public void delete();
}
学生实现类代码
package com.imooc.aop.demo5;
public class StudentDaoImpl implements StudentDao {
public void find() {
System.out.println("学生查询...");
}
public void save() {
System.out.println("学生保存...");
}
public void update() {
System.out.println("学生修改...");
}
public void delete() {
System.out.println("学生删除...");
}
}
一、基于Bean名称的自动代理
BeanNameAutoProxyCreator举例
编写前置通知方法
package com.imooc.aop.demo5;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置增强======================");
}
}
通过rescouces中的applicationConfig3.xml文件,使用上增强的方法
xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置目标类-->
<bean id="studentDao" class="com.imooc.aop.demo5.StudentDaoImpl"/>
<bean id="customerDao" class="com.imooc.aop.demo5.CustomerDao"/>
<!-- 配置增强-->
<bean id="myBeforeAdvice" class="com.imooc.aop.demo5.MyBeforeAdvice"/>
<bean id="myAroundAdvice" class="com.imooc.aop.demo5.MyAroundAdvice"/>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Dao"/>
<property name="interceptorNames" value="myBeforeAdvice"/>
</bean>
</beans>
测试类
package com.imooc.aop.demo5;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class SpringDemo5 {
@Resource(name="studentDao")
private StudentDao studentDao;
@Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void demo1(){
studentDao.find();
studentDao.save();
studentDao.update();
studentDao.delete();
customerDao.find();
customerDao.save();
customerDao.update();
customerDao.delete();
}
}
结果是
运行结果:
前置增强======================
学生查询...
前置增强======================
学生保存...
前置增强======================
学生修改...
前置增强======================
学生删除...
前置增强======================
查询客户...
前置增强======================
保存客户...
前置增强======================
修改客户...
前置增强======================
删除客户...
二、基于切面信息的自动代理
DefaultAdvisoorAutoProxyCreator举例
编写环绕通知
package com.imooc.aop.demo5;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("环绕前增强===================");
Object obj = invocation.proceed();
System.out.println("环绕后增强===================");
return obj;
}
}
通过rescouces中的applicationConfig4.xml文件,使用上增强的方法
xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置目标类-->
<bean id="studentDao" class="com.imooc.aop.demo6.StudentDaoImpl"/>
<bean id="customerDao" class="com.imooc.aop.demo6.CustomerDao"/>
<!-- 配置增强-->
<bean id="myBeforeAdvice" class="com.imooc.aop.demo6.MyBeforeAdvice"/>
<bean id="myAroundAdvice" class="com.imooc.aop.demo6.MyAroundAdvice"/>
<!--配置切面-->
<bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value="com\.imooc\.aop\.demo6\.CustomerDao\.save"/>
<property name="advice" ref="myAroundAdvice"/>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>
测试类代码,连个测试类的代码不同的地方在于
@ContextConfiguration(“classpath:applicationContext4.xml”)引用的xml文件的不同
package com.imooc.aop.demo6;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext4.xml")
public class SpringDemo6 {
@Resource(name="studentDao")
private StudentDao studentDao;
@Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void demo1(){
studentDao.find();
studentDao.save();
studentDao.update();
studentDao.delete();
customerDao.find();
customerDao.save();
customerDao.update();
customerDao.delete();
}
}
秃头的石头
发布了25 篇原创文章 · 获赞 2 · 访问量 996
私信
关注