spring tradition AOP implementation

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/shunli008/article/details/99702026

spring AOP enhanced type

Advice AOP Alliance notification defined org.aopalliance.aop.Interface.Advice
the Spring Advice in accordance with the notification connection point position of the target class methods can be divided into five categories

  1. Before advice, org.springframework.aop.MethodBeforeAdvice

    In the former method of enhancing target

  2. After returning advice org.springframework.aop.AfterReturningAdvice

    Before the implementation of enhanced target execution method

  3. Around advice org, aopalloance.intercept.MethodInterceptor

    Before and after the target method to enhance the implementation of the Executive

  4. An exception is thrown notice org.springframework.aop.ThrowsAdvice

    After the implementation of enhanced method throws an exception

  5. Introducing notification org.springframework.aop.InteroductionInterceptor

    Adding new methods and properties in the target class (not used)

aop aspect type

  • Advisor: on behalf of the general section, Advice itself is a section of the target class to intercept all methods
  • PointcutAdvisor: section on behalf of a specific cut-off point, you can specify which methods to intercept the target class
  • IntrodcutionAdvisor: representatives introducing section for introducing the use of notification section (not used)

First need to add in the configuration file
Here Insert Picture Description

Is then disposed in the 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 in

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("前置增强......");
		
	}

}

During 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();

    }
}

Guess you like

Origin blog.csdn.net/shunli008/article/details/99702026