zoukankan      html  css  js  c++  java
  • Spring AOP通过注解的方式设置切面和切入点

    切面相当于一个功能的某一个类,切入点是这个类的某部分和需要额外执行的其他代码块,这两者是多对多的关系,在代码块处指定执行的条件。

    Aspect1.java

    package com.yh.aop.schema.advice.myAspect;
    
    public class Aspect1 {
        
        public void doIt() {
            System.out.println("Aspect1:do");
        }
    }

    PointCut1.java

    package com.yh.myPointCut;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    public class PointCut1 {
    
        @Before("execution(* com.yh.aop.schema.advice.myAspect.Aspect1.*(..))")
        public void before() {
            System.out.println("PointCut1:before");
        }
    
    }

    applicationContext.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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        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.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
    
         <context:component-scan base-package="com.yh"></context:component-scan>
         <context:component-scan base-package="com.yh.myPointCut"></context:component-scan>
     
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        
        <bean id="aspect1" class="com.yh.aop.schema.advice.myAspect.Aspect1"></bean>
        
    </beans>

    MyTest.java

    @Test
    public void testPointCut(){
        String xmlPath="applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
        Aspect1 aspect1 = (Aspect1) context.getBean("aspect1");
        aspect1.doIt();
    }
  • 相关阅读:
    Java程序员:一整个项目的具体开发流程介绍
    JAVA常用API整理
    Java开发人员必知必会的20种常用类库和API
    SpringBoot_web开发【实验】-员工列表-链接高亮&列表完成
    luogu P1754 球迷购票问题 |动态规划
    luogu P1566 加等式 |背包问题方案数
    luogu P1564 膜拜 |动态规划
    luogu P1509 找啊找啊找GF |背包
    P1474 货币系统 Money Systems |背包方案数
    cloudera安装报错 socket.gaierror: [Errno -2] Name or service not known
  • 原文地址:https://www.cnblogs.com/YeHuan/p/11110208.html
Copyright © 2011-2022 走看看