spring 传统AOP实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/shunli008/article/details/99702026

spring AOP增强类型

AOP联盟为通知Advice定义了org.aopalliance.aop.Interface.Advice
Spring按照通知Advice在目标类方法的连接点位置,可以分为5类

  1. 前置通知, org.springframework.aop.MethodBeforeAdvice

    在目标方法前实施增强

  2. 后置通知 org.springframework.aop.AfterReturningAdvice

    在目标方法执行前实施增强

  3. 环绕通知 org,aopalloance.intercept.MethodInterceptor

    在目标方法执行前后实施增强

  4. 异常抛出通知 org.springframework.aop.ThrowsAdvice

    在方法抛出异常后实施增强

  5. 引介通知 org.springframework.aop.InteroductionInterceptor

    在目标类中添加一些新的方法和属性(不常用)

aop切面类型

  • Advisor:代表一般切面,Advice本身就是一个切面,对目标类所有方法进行拦截
  • PointcutAdvisor:代表具体切点的切面,可以指定拦截目标类哪些方法
  • IntrodcutionAdvisor:代表引介切面,针对引介通知而使用切面(不常用)

首先需要在配置文件中加入
在这里插入图片描述

然后在applicationContext.xml中配置

<!-- 配置目标类 -->
<bean id="studentDao" class="com.shunli.ioc.demo6.StudentDaoImpl"></bean>

<!-- 前置通知类型 -->
<bean id = "myBeforeAdvice" class="com.shunli.ioc.demo6.MyBeforeAdvice"></bean>

<!-- Spring的AOP产生代理对象 -->
<bean id="studentDaoProxy" class ="org.springframework.aop.framework.ProxyFactoryBean">
	<!-- 配置目标类 -->
	<property name="target" ref="studentDao"></property>
	<!-- 实现的接口 -->
	<property name="proxyInterfaces" value = "com.shunli.ioc.demo6.StudentDao"></property>
	<!-- 采用拦截的名称 -->
	<property name="interceptorNames" value="myBeforeAdvice"></property>
</bean>

StudentDao.java中

package com.shunli.ioc.demo6;

public interface StudentDao {
	public void find();
	
	public void save();
	
	public void update();
	
	public void delete();
}

StudentDaoImpl.java

package com.shunli.ioc.demo6;

public class StudentDaoImpl implements StudentDao{

	@Override
	public void find() {
		System.out.println("find.....");
		
	}

	@Override
	public void save() {
		System.out.println("save.....");
		
	}

	@Override
	public void update() {
		System.out.println("update.....");
		
	}

	@Override
	public void delete() {
		System.out.println("delete.....");
		
	}

}

MyBeforeAdvice.java

package com.shunli.ioc.demo6;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyBeforeAdvice implements MethodBeforeAdvice{

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		System.out.println("前置增强......");
		
	}

}

demo中

package com.shunli.ioc.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:applicationContext.xml")
public class Demo {
	
    @Resource(name="studentDaoProxy")
    private StudentDao studentDao;

    @Test
    public void demo1(){
    	System.out.println("----------------------------------");
        studentDao.find();
        studentDao.save();
        studentDao.update();
        studentDao.delete();

    }
}

猜你喜欢

转载自blog.csdn.net/shunli008/article/details/99702026