美文网首页
spring aop指定切面

spring aop指定切面

作者: simplerandom | 来源:发表于2020-07-23 17:41 被阅读0次
    <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.16.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.7</version>
            </dependency>
        </dependencies>
    
    public class Log {
        public void before(){
            System.out.println("aop before");
        }
    }
    
     <bean id="hello" class="org.spring.Hello"></bean>
        <bean id="log" class="org.spring.Log"></bean>
        <aop:config>
            <aop:aspect id="log" ref="log">
                <aop:before method="before" pointcut="execution(public void org.spring.Hello.aop())"></aop:before>
            </aop:aspect>
        </aop:config>
    

    解释
    每次执行Hello类中的aop方法都会触发切面Log类中的before方法先执行

    @Pointcut("execution(* com.cjm.model..*.*(..))")
    

    切面方法

    package org.spring;
    
    public class Log {
        public void before(){
            System.out.println("aop before");
        }
        public void after(){
            System.out.println("aop after");
        }
        public void exp(){
            System.out.println("aop ex");
        }
        public void fina(){
            System.out.println("aop fina");
        }
    
    }
    
    <aop:aspect id="log" ref="log">
                <aop:before method="before" pointcut="execution(public void org.spring.Hello.aop())"></aop:before>
                <aop:after-returning method="after" pointcut="execution(public void org.spring.Hello.*(..))"></aop:after-returning>
                <aop:after-throwing method="exp" pointcut="execution(public void org.spring.Hello.*(..))"></aop:after-throwing>
                <aop:after method="fina" pointcut="execution(public void org.spring.Hello.*(..))"></aop:after>
            </aop:aspect>
    

    相关文章

      网友评论

          本文标题:spring aop指定切面

          本文链接:https://www.haomeiwen.com/subject/ooihlktx.html