Spring aop的几种方式

工程下载地址:https://gitee.com/wuhan1/spring-parent.git 的spring-02
工程结构

1.实现接口

分别实现相应的接口,后置通知

环绕通知

对应的xml配置,applicationContext.xml

<!--suppress SpringFacetInspection -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

    <bean id="studentServiceImpl" class="com.xqc.com.StudentServiceImpl" ></bean>

    <!--前置通知-->
    <bean id="logBefore" class="com.xqc.xml.LogBefore"></bean>
    <!--后置通知-->
    <bean id="logAfter" class="com.xqc.xml.LogAfter"></bean>
    <!--异常通知-->
    <bean id="logThrow" class="com.xqc.xml.LogThrow"></bean>
    <!--环绕通知-->
    <bean id="logAround" class="com.xqc.xml.LogAround"></bean>

    <!--前置通知-->
    <aop:config>
        <!--声明切入点-->
        <aop:pointcut id="beforePointcut" expression="execution(* com.xqc.com.StudentServiceImpl.*(..))"></aop:pointcut>
        <!--通知类-->
        <aop:advisor advice-ref="logBefore" pointcut-ref="beforePointcut"></aop:advisor>
    </aop:config>

    <!--后置通知-->
    <aop:config>
        <aop:pointcut id="afterPointcut" expression="execution(* com.xqc.com.StudentServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="logAfter" pointcut-ref="afterPointcut"></aop:advisor>
    </aop:config>

    <!--异常通知-->
    <aop:config>
        <aop:pointcut id="throwPointcut" expression="execution(* com.xqc.com.StudentServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="logThrow" pointcut-ref="throwPointcut"></aop:advisor>
    </aop:config>

    <!--环绕通知-->
    <aop:config>
        <aop:pointcut id="aroundPointcut" expression="execution(* com.xqc.com.StudentServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="logAround" pointcut-ref="aroundPointcut"></aop:advisor>
    </aop:config>
</beans>

2.基于Schema,声明一个类,然后写前置、后置、环绕等通知

在xml中声明,那个是前置、后置通知等,对应的xml文件,schema-applicationContext.xml

<!--suppress SpringFacetInspection -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

    <bean id="studentServiceImpl" class="com.xqc.com.StudentServiceImpl" ></bean>

    <!--schma方式通知-->
    <bean id="logBeforeSchema" class="com.xqc.schema.LogBeforeSchema"></bean>

    <!--前置通知-->
    <aop:config>
        <!--声明切入点-->
        <aop:pointcut id="schemaPointcut" expression="execution(* com.xqc.com.StudentServiceImpl.*(..))"></aop:pointcut>
        <aop:aspect ref="logBeforeSchema">
            <aop:before method="before" pointcut-ref="schemaPointcut" /><!--前置通知-->
            <aop:after-returning method="afterReturning" pointcut-ref="schemaPointcut"/><!--后置通知-->
        </aop:aspect>
    </aop:config>
</beans>

3.注解形式
新建一个类,加上@Aspect注解,通知加上@Component,扫描此类注入到容器中

@Component
@Aspect
public class AnnAspectj {

    /**
     * 前置通知
     */
    @Before("execution(* com.xqc.com.StudentServiceImpl.*(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("注解通知,before。。。。");
    }

    /**
     * 最终通知
     */
    @AfterReturning("execution(* com.xqc.com.StudentServiceImpl.*(..))")
    public void After(JoinPoint joinPoint){
        System.out.println("注解通知,AfterReturning。。。。");
    }

    /**
     * 最终通知
     */
    @Around("execution(* com.xqc.com.StudentServiceImpl.*(..))")
    public Object Around(ProceedingJoinPoint joinPoint){
        System.out.println("注解通知,Around之前。。。。");
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
            System.out.println("注解通知,Around之后。。。。");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }

配置文件,annotation-applicationContext.xml

<!--suppress SpringFacetInspection -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

    <bean id="studentServiceImpl" class="com.xqc.com.StudentServiceImpl" ></bean>
    <!--开启注解支持-->
   <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <!--切面类扫描-->
    <context:component-scan base-package="com.xqc.ann"></context:component-scan>
</beans>

猜你喜欢

转载自blog.csdn.net/dhj199181/article/details/108610685
今日推荐