spring03 ----- use AOP in the spring

In Spring default JDK dynamic proxy AOP achieve programming, org.springframework.aop.framework.ProxyFactoryBeancreate a proxy is the most basic way to Spring AOP implementation.



First, the type of notification

Spring connection point informed the target position in accordance with the class method, the notification may be divided into six types:

type name effect Scenarios
Surround notification (org.aopalliance.intercept.MethodInterceptor) Before and after performing certain method for performing enhanced embodiment It can be applied to logging, transaction processing and other functions.
Pre-notification (org.springframework.aop.MethodBeforeAdvice) Before the implementation of enhanced target execution method It can be applied to rights management.
Rear return notification (the org.springframework.aop.AfterReturningAdvice) After the successful execution of the target method implementation of enhanced It can be applied to close the stream, delete temporary files and other functions.
Rear notification (org.springframework.aop.AfterAdvice) After the implementation of enhanced target method execution, and post-return different notice is that regardless of whether the implementation of such notice to be abnormal It can be applied to free up resources.
Exception notification (org.springframework.aop.ThrowsAdvice) After the implementation of enhanced method throws an exception It can be applied to handle exceptions, logging functions.
Introduction advice (org.springframework.aop.IntroductionInterceptor) Add some new methods and properties in the target class It may be applied to the modified target class (class enhancement). Spring AOP only supports method-level enhancements, so this type of notification can be understood only as a.



Second, the type of cut

Section type effect
General section (Advisor) For all methods to intercept the target class
Section (PointcutAdvisor) having a tangent point You can specify which methods to intercept the target class
Introducing section (an IntroductionAdvisor) Notice for introducing the use of section



Three, ProxyFactoryBean proxy factory

ProxyFactoryBean is org.springframework.beans.factory.FactoryBeanthe interface implementation class, this class has been provided to us by the spring, so we only need to be configured in the configuration file on the line. FactoryBean responsible for instantiating a Bean instance, ProxyFactoryBean responsible for creating a proxy for other instances Bean instance. ProxyFactoryBean class common properties as follows:

Attributes Explanation
target Acting audience
proxyInterfaces Acting interface list needs to be implemented, and if multiple interfaces, can be assigned using the following format:<list> <value></value> </list>
InterceptorNames We need to weave into the goal Advice
proxy target class Whether or not the interface class of agents, the default is false, the JDK dynamic proxy; when true, the use of dynamic proxies CGLIB
singleton Examples of the agent whether the returned single embodiment, default is true
optimize When set to true force the use of dynamic proxy CGLIB



Four, Advisor to achieve general section

  1. Import-related dependence AOP Alliance and Spring-aop in the pom.xml

    <!-- AOP联盟依赖 -->
    <dependency>
    	<groupId>aopalliance</groupId>
    	<artifactId>aopalliance</artifactId>
    	<version>1.0</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aop</artifactId>
    	<version>5.0.2.RELEASE</version>
    </dependency>
    
  2. Creating StuDao StuDaoImpl interfaces and implementation class
    StuDao interfaces:

    public interface StuDao {
    	public void save();
    	public void modify();
    	public void delete();
    }
    

    StuDaoLmpl implementation class:

    public class StuDaoImpl implements StuDao {
    	@Override
    	public void save() {
        	System.out.println("保存");
    	}
    
    	@Override
    	public void modify() {
        	System.out.println("修改");
    	}
    
    	@Override
    	public void delete() {
        	System.out.println("删除");
    	}
    }
    
  3. Create a section class MyBeforeAdvice

    import org.springframework.aop.MethodBeforeAdvice;
    import java.lang.reflect.Method;
    
    public class MyBeforeAdvice implements MethodBeforeAdvice {
    	@Override
    	public void before(Method method, Object[] objects, Object o) throws Throwable {
        	System.out.println("前置增强========");
    	}
    }
    
  4. In the configuration section and specify the proxy applicationContext.xml

    <!--配置目标类-->
    <bean id="stuDao" class="aop.StuDaoImpl"></bean>
    
    <!--配置前置通知类型-->
    <bean id="myBeforeAdvice" class="aop.MyBeforeAdvice"></bean>
    
    <!--Spring AOP 生成代理对象-->
    <bean id="stuDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    
    	<!--配置目标类-->
    	<property name="target" ref="stuDao"></property>
    	
    	<!--代理实现的接口-->
    	<property name="proxyInterfaces" value="aop.StuDao"></property>
    	
    	<!--采取拦截的名称-->
    	<property name="interceptorNames" value="myBeforeAdvice"></property>
    </bean>
    
  5. Create a test class

    @Test
    public void demo(){
    	ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    	StuDao stuDao = (StuDao) app.getBean("stuDaoProxy");
    	stuDao.save();
    	stuDao.modify();
    	stuDao.delete();
    }
    



Five, PointcutAdvisor cut point cut to achieve

Use as ordinary Advice section, will all methods to intercept the target class, not flexible enough, using the cut with a cut-off point in the actual development often.
Common PointcutAdvisor implementation class:

DefaultPointcutAdvisor most common type of cut, it can be defined by a combination of any Pointcut and Advice section;
JdkRegexpMethodPointcut construct a regular expression tangent point (recommended)

  1. Creating StuDao StuDaoImpl interfaces and implementation class
    StuDao interfaces:
    public interface StuDao {
    	public void save();
    	public void modify();
    	public void delete();
    }
    
    StuDaolmpl implementation class:
    public class StuDaoImpl implements StuDao {
    	@Override
    	public void save() {
        	System.out.println("保存");
    	}
    
    	@Override
    	public void modify() {
        	System.out.println("修改");
    	}
    
    	@Override
    	public void delete() {
        	System.out.println("删除");
    	}
    }
    
  2. Creating a class that implements section around advice
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class MyAspect implements MethodInterceptor {
    	@Override
    	public Object invoke(MethodInvocation m) throws Throwable {
        	//前置增强方法
        	check();
        	//执行目标方法
        	Object obj = m.proceed();
        	//后置增强方法
        	log();
        	return obj;
    	}
    
    	public void check(){
        	System.out.println("模拟权限控制");
    	}
    	public void log(){
        	System.out.println("模拟日志记录");
    	}
    }
    
  3. In the configuration section and specify the proxy applicationContext.xml
    <!--配置目标类-->
    <bean id="stuDao" class="aop.StuDaoImpl"></bean>
    <!--配置通知-->
    <bean id="myAspect" class="aop.MyAspect"></bean>
    <!--配置带切入点的切面-->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
     	<!--pattern中配置正则表达式:.表示任意字符 *表示任意次数-->
     	<property name="pattern" value=".*"></property>
     	<property name="advice" ref="myAspect"></property>
    </bean>
    <!--Spring AOP 生成代理对象-->
    <bean id="stuDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    	 <!--配置目标类-->
    	 <property name="target" ref="stuDao"></property>
    	 <!--代理实现的接口-->
    	 <property name="proxyInterfaces" value="aop.StuDao"></property>
    	 <!--采取拦截的名称-->
    	 <property name="interceptorNames" value="myAdvisor"></property>
    </bean>
    
  4. Create a test class
    @Test
    public void demo(){
    	ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    	StuDao stuDao = (StuDao) app.getBean("stuDaoProxy");
    	stuDao.save();
    	stuDao.modify();
    	stuDao.delete();
    }
    

Original Address: the Spring Framework-based learning 07-- blog traditional proxy class AOP implementation of Panya

Guess you like

Origin blog.csdn.net/f2764052703/article/details/90447409