前置增强
配置文件1
<?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="custoemrDao" class="com.neuedu.demo4.CustoemrDaoImpl"></bean>
<!-- 配置增强/通知类 -->
<bean id="beforeAdvice" class="com.neuedu.demo4.MyBeforeAdvice"></bean>
<!-- Spring支持配置生成代理 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象 -->
<property name="target" ref="custoemrDao"></property>
<!-- 设置实现的接口,value中写接口的全路径 -->
<property name="proxyInterfaces" value="com.neuedu.demo4.CustomerDao"></property>
<!-- 配置一个切面 -->
<property name="interceptorNames" value="beforeAdvice"></property>
<!-- 强制使用CGLIB代理 -->
<!-- <property name="optimize" value="true"></property> -->
</bean>
<!-- 带有切点的切面 -->
<!-- 定义目标类 -->
<bean id="orderDao" class="com.neuedu.demo5.OrderDao"></bean>
<!-- 定义增强 -->
<bean id="aroundAdvice" class="com.neuedu.demo5.MyAroundAdvice"></bean>
<!-- 定义切面 -->
<bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 定义切点 -->
<property name="patterns" value=".*add.*,.*fi.*"></property>
<!-- 应用通知/增强 -->
<property name="advice" ref="aroundAdvice"></property>
</bean>
<!-- 配置带有切点的切面的代理 -->
<bean id="orderDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="orderDao"></property>
<!-- 针对类的代理 -->
<property name="proxyTargetClass" value="true"></property>
<!-- 配置切面 -->
<property name="interceptorNames" value="myPointcutAdvisor"></property>
</bean>
</beans>
接口
package com.neuedu.demo4;
public interface CustomerDao {
public void add();
public void delete();
public void update();
public void find();
}
实现类
package com.neuedu.demo4;
//目标对象
public class CustoemrDaoImpl implements CustomerDao {
@Override
public void add() {
System.out.println("添加客户。。");
}
@Override
public void delete() {
System.out.println("删除客户。。");
}
@Override
public void update() {
System.out.println("修改客户。。");
}
@Override
public void find() {
System.out.println("查询客户。。");
}
}
前置增强/通知类
package com.neuedu.demo4;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
//通知/增强类(带有类型的:前置、后置、环绕、异常)
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] arg1, Object target) throws Throwable {
System.out.println("前置增强。。。");
}
}
测试类
package com.neuedu.demo4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest4 {
@Autowired
// @Qualifier("custoemrDao")
@Qualifier("customerDaoProxy")
private CustomerDao customerDao;
@Test
public void demo1(){
customerDao.add();
customerDao.delete();
customerDao.update();
customerDao.find();
}
}
配置文件与第一个配置文件同
目标类
package com.neuedu.demo5;
//目标类,没有实现接口
public class OrderDao {
public void add() {
System.out.println("添加订单。。");
}
public void delete() {
System.out.println("删除订单。。");
}
public void update() {
System.out.println("修改订单。。");
}
public void find() {
System.out.println("查询订单。。");
}
}
环绕增强类
package com.neuedu.demo5;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("环绕前增强。。。");
Object result = methodInvocation.proceed();
System.out.println("环绕后增强。。。");
return result;
}
}
测试类
package com.neuedu.demo5;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest5 {
@Autowired
@Qualifier("orderDaoProxy")
private OrderDao orderDao;
@Test
public void demo1(){
orderDao.add();
orderDao.delete();
orderDao.update();
orderDao.find();
}
}
自动代理
BeanNameAutoProxyCreator :按名称生成代理
配置文件
<?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.neuedu.demo4.CustoemrDaoImpl"></bean>
<bean id="orderDao" class="com.neuedu.demo5.OrderDao"></bean>
<!-- 配置增强/通知类 -->
<bean id="beforeAdvice" class="com.neuedu.demo4.MyBeforeAdvice"></bean>
<bean id="aroundAdvice" class="com.neuedu.demo5.MyAroundAdvice"></bean>
<!-- 自动代理:按名称的代理 基于bean生命周期的后处理Bean,后处理Bean不需要配置ID-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Dao"></property>
<property name="interceptorNames" value="beforeAdvice"></property>
</bean>
</beans>
测试类
package com.neuedu.demo6;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.neuedu.demo4.CustomerDao;
import com.neuedu.demo5.OrderDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext1.xml")
public class SpringTest6 {
@Autowired
@Qualifier("customerDao")
private CustomerDao customerDao;
@Autowired
@Qualifier("orderDao")
private OrderDao orderDao;
@Test
public void demo1(){
orderDao.add();
orderDao.delete();
orderDao.update();
orderDao.find();
customerDao.find();
}
}
DefaultAdvisorAutoProxyCreator :根据切面中定义的信息生成代理
配置文件
<?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.neuedu.demo4.CustoemrDaoImpl"></bean>
<bean id="orderDao" class="com.neuedu.demo5.OrderDao"></bean>
<!-- 配置增强/通知类 -->
<bean id="beforeAdvice" class="com.neuedu.demo4.MyBeforeAdvice"></bean>
<bean id="aroundAdvice" class="com.neuedu.demo5.MyAroundAdvice"></bean>
<!-- 定义一个带有切点的切面 -->
<bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns" value=".*ad.*"></property>
<property name="advice" ref="aroundAdvice"></property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>
测试类
package com.neuedu.demo7;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.neuedu.demo4.CustomerDao;
import com.neuedu.demo5.OrderDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext2.xml")
public class SpringTest7 {
@Autowired
@Qualifier("customerDao")
private CustomerDao customerDao;
@Autowired
@Qualifier("orderDao")
private OrderDao orderDao;
@Test
public void demo1(){
orderDao.add();
orderDao.delete();
orderDao.update();
orderDao.find();
customerDao.find();
customerDao.add();
}
}
网友评论