一、AOP之传统手动代理——不带切入点的切面





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 {
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.demo3;
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("前置增强======================");
}
}
<?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"/>
<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>
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="studentDaoProxy")
private StudentDao studentDao;
@Test
public void demo1(){
studentDao.find();
studentDao.save();
studentDao.update();
studentDao.delete();
}
}
运行结果:
前置增强======================
学生查询...
前置增强======================
学生保存...
前置增强======================
学生修改...
前置增强======================
学生删除...
二、AOP之传统手动代理——带切入点的切面

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("删除客户...");
}
}
<?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">
<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>
package com.imooc.aop.demo4;
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;
}
}
运行结果:
查询客户...
环绕前增强===================
保存客户...
环绕后增强===================
修改客户...
环绕前增强===================
删除客户...
环绕后增强===================