zoukankan      html  css  js  c++  java
  • SSM框架—Spring AOP之基于注解的声明式AspectJ(Demo)

    项目结构

    XML

    <?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--自动搜索切面类-->
        <context:component-scan base-package="learning.aop"/>
    
        <!--启动@AspectJ支持 默认是false-->
        <aop:aspectj-autoproxy expose-proxy="true"/>
    </beans>

    Person.java

    package learning.aop;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Person {
        public void eat() {
            System.out.println("吃早餐哈");
        }
    }

    AdivceMethod.java

    package learning.aop;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    public class AdivceMethod {
        @Before("execution(* learning.aop.Person.*(..))")
        public void beforeEat() {
            System.out.println("吃饭之前先蹦迪");
        }
    }

    AopTest.java

    package learning.aop;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AopTest {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("/learning/spring/aop.xml");
            Person  person = (Person) context.getBean("person");
            person.eat();
        }
    }

  • 相关阅读:
    rancher 2.X 搭建小型web集群+mysql主从复制
    harbor 仓库搭建
    k8s 集群搭建
    oracle sql命令
    IIS实现反向代理
    高并发的大型网站架构设计
    .net core集成vue
    使用TFS玩转Docker自动化部署
    动态创建IIS站点
    网站架构设计(草稿)
  • 原文地址:https://www.cnblogs.com/dzcici/p/10137106.html
Copyright © 2011-2022 走看看