package com.imooc.aop.demo3; public interface StudentDao { public void find(); public void save(); public void update(); public void delete(); }
package com.imooc.aop.demo3; public class StudentDaoImpl implements StudentDao { @Override public void find() { System.out.println("学生查询..."); } @Override public void save() { System.out.println("学生保存。。。"); } @Override public void update() { System.out.println("学生修改。。。"); } @Override public void delete() { System.out.println("学生删除。。。。"); } }
package com.imooc.aop.demo3; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class MyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("前置增强========="); } }
package com.imooc.aop.demo3; 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:applicationContext.xml") public class SpringDemo3 { // @Resource(name="studentDao") @Resource(name="studentDaoProxy") private StudentDao studentDao; @Test public void demo1(){ studentDao.find(); studentDao.save(); studentDao.update(); studentDao.delete();; } }
<?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.demo3.StudentDaoImpl"/> <!-- 前置通知类型 --> <bean id="myBeforeAdvice" class="com.imooc.aop.demo3.MyBeforeAdvice"/> <!--Spring 的AOP产生代理对象--> <bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--配置目标类--> <property name="target" ref="studentDao"/> <!--实现的接口--> <property name="proxyInterfaces" value="com.imooc.aop.demo3.StudentDao"/> <!--采用拦截的名称--> <property name="interceptorNames" value="myBeforeAdvice"/> <!--<property name="optimize" value="true"></property>--> </bean> </beans>
===============pom.xml======================== <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies>
上面的例子是以增强作为切面。下面是增强+切入点=切面
package com.imooc.aop.demo4; 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("删除客户..."); } }
public class MyAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("环绕前增强================="); Object obj=invocation.proceed(); System.out.println("环绕后增强================="); return obj; } }
<?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="customerDao" class="com.imooc.aop.demo4.CustomerDao"/> <!--配置通知=========--> <bean id="myAroundAdvice" class="com.imooc.aop.demo4.MyAroundAdvice"/> <!--一般的切面是使用通知作为切面的,因为要对目标类的某个方法进行增强,需要配置一个带有切入点的切面--> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!--pattern中配置正则表达式:.代表任意字符,*代表任意次数--> <!--<property name="pattern" value=".*save.*"/>--> <property name="patterns" value=".*save.*,.*delete.*"/> <property name="advice" ref="myAroundAdvice"/> </bean> <!--配置产生代理--> <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerDao"/> <property name="proxyTargetClass" value="true"/> <property name="interceptorNames" value="myAdvisor"/> </bean> </beans>
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext2.xml") public class SpringDemo4 { @Resource(name="customerDaoProxy") private CustomerDao customerDao; @Test public void demo1(){ customerDao.find(); customerDao.save(); customerDao.update(); customerDao.delete(); } }