Detailed spring (c) - AOP

AOP: notes to complete

1, adding dependent jar files

2, create a class section

package com.zhiyou.zjc.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Component
@Aspect
public class CountAspect {    
    @Before("execution(* com.zhiyou.zjc.aspect.CountDaoImp.*(..))")
    public void logbefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("==========before=========="+joinPoint.getSignature().getName()+Arrays.asList(args));
    }
    @After("execution(* com.zhiyou.zjc.aspect.CountDaoImp.*(..))")
    public void logafter() {
        System.out.println("=====after=======");
    }
    
}

 

3, cut open in the spring configuration file notes

<context:component-scan base-package="com.zhiyou.zjc.aspect"></context:component-scan>
<aop:aspectj-autoproxy/>

 

4, the test

package com.zhiyou.zjc.aspect;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
        CountDao coImp = (CountDao) app.getBean("imp2");
        coImp.add(2, 3);
    }  
    
}

 

AOP: xml-based way

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/aop " > 
    <-! program class definition is notified -> 
    < the bean ID = "IMP2" class = "com.zhiyou.zjc.aspect.CountDaoImp2" > </ the bean > 
    <-! the bean class definitions section -> 
    < the bean ID = "Aspect" class = "com.zhiyou. zjc.aspect.CountAspect2 "  /> 
    
    ! <- configuration section of the xml file -> 
    < AOP: config > 
        <-! custom expression cut-off point -> 
        < AOP:pointcut expression =" Execution (* com.zhiyou.zjc. .. Aspect * * (..)) " ID =" the pointcut " />     
        ! <- defined aspects ->     
        < AOP: Aspect ID ="lodaspect" ref="aspect">
            <!-- 定义前置通知 -->
            <aop:before method="logbefore" pointcut-ref="pointcut"/>
            <!-- 定义后置通知 -->
            <aop:after method="logafter" pointcut-ref="pointcut"/>
        </aop:aspect>
        
        
    </aop:config>
</beans>

 

Guess you like

Origin www.cnblogs.com/zjc364259451/p/11503356.html