Spring学习-18:Spring的AOP:不带有切点的切面

Spring中的AOP的学习,从两个角度:1、Spring的传统AOP(不作重点)2、Spring的AspectJ(重点)

一、Spring中的传统AOP

AOP:不是由Spring定义,而是由AOP联盟来定义。AOP联盟为通知Advice定义了org.aopalliance.Interface.Advice。

Spring按照通知Advice在目标方法的连接点位置,可以分为五类

前置通知:org.springframework.aop.MethodBeforeAdvice,在目标方法执行之前实施增强

后置通知:org.springframework.aop.AfterReturningAdvice,在目标方法执行之后实施增强

环绕通知:org.aopalliance.intercept.MethodInterceptor,在目标方法执行前后实施增强

异常抛出通知:org.springframework.aop.ThrowsAdvice,在方法抛出异常之后实施增强

引介通知:org.springframework.aop.IntroductionInterceptor,在目标类中添加一些新的方法和属性(类级别的增强,本次专题不涉及)

Spring中切面的类型:

Advisor:Spring中的传统切面。

Aspect:都是有一个切点和一个通知的组合

Advisor:多个切点和多个通知的组合

lAdvisor : 代表一般切面,Advice本身就是一个切面,对目标类所有方法进行拦截(*不带有切点的切面)

lPointcutAdvisor : 代表具有切点的切面,可以指定拦截目标类哪些方法带有切点的切面,针对某个方法进行拦截

l IntroductionAdvisor : 代表引介切面,针对引介通知而使用切面(不要求掌握)
下面,做个简单的AOP开发加深理解:
针对所有方法的增强
1、导入相应的jar包:Spring开发基础包(详见第二讲)、spring-aop-4.3.7.RELEASE.jar、com.springsource.org.aopalliance-1.0.0.jar(AOP联盟包)
2、编写一个接口Customer:
package com.js.aopStu;

public interface CustomerDao {
	public void add();
	public void delete();
	public void update();
	public void find();
}
编写实现类CustomerImpl:
package com.js.aopStu;

public class CustomerImpl 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("查询客户...");
	}

}
3、编写增强的代码。新建一个类MyBeforeAdvice,以前置增强为例。
package com.js.aopStu;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

/**
 * 前置增强
 * 实现指定接口
 * @author JiangShuai
 *
 */
public class MyBeforeAdvice implements MethodBeforeAdvice{
	/**
	 * method:执行的方法
	 * args:参数
	 * target:目标对象
	 */
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		System.out.println("前置增强===");
	}
	
}
4、不使用增强的情况下,我们来测试下,新建测试类:
package com.js.aopStu;

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("classpath:applicationContext.xml")
public class TestDemo1 {
	@Autowired
	@Qualifier("customerDao")
	private CustomerDao customerDao;
	@Test
	public void Demo1(){
		customerDao.add();
		customerDao.delete();
		customerDao.find();
		customerDao.update();
	}
}
配置applicationContext.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"
    xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
        <!-- 定义目标对象 -->
        <bean id="customerDao" class="com.js.aopStu.CustomerImpl"></bean>
        <!-- 定义增强 -->
        <bean id="beforeAdice" class="com.js.aopStu.MyBeforeAdvice"></bean>
        <!-- 生成代理 -->
        
</beans>


运行测试,发现正常运行。
5、我们来配置代理,基于ProxyFactoryBean类,底层自动选择使用JDK的动态代理还是CGLIB的代理。
 <!-- Spring支持配置来生成代理 -->
        <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        	<property name=""></property>
        </bean>

此外,我们还需要配置一些属性,不需要都设置。
lProxyFactoryBean常用可配置属性
target : 代理的目标对象
proxyInterfaces : 代理要实现的接口
•如果多个接口可以使用以下格式赋值

<list>

    <value></value>

    ....

</list>

proxyTargetClass : 是否对类代理而不是接口,设置为true时,使用CGLib代理
interceptorNames : 需要织入目标的Advice
singleton : 返回代理是否为单实例,默认为单例
optimize : 当设置为true时,强制使用CGLib
proxyInterfaces、proxyTargetClass二者互斥,不能同时存在
修改我们的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
        <!-- 定义目标对象 -->
        <bean id="customerDao" class="com.js.aopStu.CustomerImpl"></bean>
        <!-- 定义增强 -->
        <bean id="beforeAdice" class="com.js.aopStu.MyBeforeAdvice"></bean>
        <!-- Spring支持配置来生成代理 -->
        <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 设置目标对象 -->
        	<property name="target" ref="customerDao"></property>
        	<!-- 设置实现的接口,value中写接口的全路径 -->
        	<property name="proxyInterfaces" value="com.js.aopStu.CustomerDao"></property>
        	<!-- 配置需要拦截的,一定是value,此处对customerDao中的所有方法拦截 -->
        	<property name="interceptorNames" value="beforeAdice"></property>
        </bean>
</beans>

这时,进行测试应该应用我们的代理对象:

运行测试,结果如下:

实现了前置增强。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值