zoukankan      html  css  js  c++  java
  • Spring @Aspect切面参数传递

    Spring @Aspect切面参数传递:

    Xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <!-- 这个声明会创建AnnotationAwareAspectJAutoProxyCreator,进行切面Bean的代理 -->
        <aop:aspectj-autoproxy />
        <!-- 必须将切面类声明为一个Bean -->
        <bean id="magician" class="com.stono.sprtest3.Magician"></bean>
        <bean id="volunteer" class="com.stono.sprtest3.Volunteer"></bean>
    </beans>

    AppBean:

    package com.stono.sprtest3;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class AppBeans12 {
        public static void main(String[] args) {
            @SuppressWarnings("resource")
            ApplicationContext context = new ClassPathXmlApplicationContext("appbeans12.xml");
            // 在AOP情况下,如果有接口就必须用接口来接,否则会报ClassCastException;
            Thinker bean = (Thinker) context.getBean("volunteer");
            bean.thinkOfSomething("volunteer think of something");
            MindReader bean2 = (MindReader) context.getBean("magician");
            String thoughts = bean2.getThoughts();
            System.out.println(thoughts);
        }
    }

    切面:

    package com.stono.sprtest3;
    public interface MindReader {
        void interceptThoughts(String thoughts);
        String getThoughts();
    }
    package com.stono.sprtest3;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    @Aspect
    public class Magician implements MindReader {
        @Pointcut("execution(* com.stono.sprtest3.Thinker.thinkOfSomething(String)) && args(thoughts)")
        public void thinking(String thoughts) {
        }
        private String thoughts;
        @Override
        @Before("thinking(thoughts)")
        public void interceptThoughts(String thoughts) {
            System.out.println("Intercepting volunteer's thoughts");
            this.thoughts = thoughts;
        }
        @Override
        public String getThoughts() {
            return thoughts;
        }
    }

    POJO:

    package com.stono.sprtest3;
    public interface Thinker {
        void thinkOfSomething(String thoughts);
    }
    package com.stono.sprtest3;
    public class Volunteer implements Thinker {
        private String thoughts;
        @Override
        public void thinkOfSomething(String thoughts) {
            this.thoughts = thoughts;
        }
        public String getThoughts() {
            return thoughts;
        }
    }
  • 相关阅读:
    软件设计和开发是手艺活也是艺术活
    学界老师和业界专业人员的紧密合作才能促进软件设计开发教学的进步
    最简单的 GitExtensions 教程(持续更新中)
    最简单的 IntelliJ IDEA 中使用 GitHub 进行版本控制教程(持续更新中)
    工作室成员 GitHub 地址集中贴(按发布时间先后排序)
    使用 Visual Studio Code 运行 C# 及 Java 程序
    推荐一个非常好的 IntelliJ IDEA 教程
    Commit message 和 Change log 编写指南(转自阮一峰的博客)
    关于编码规范的延伸资料(来自于福州大学陈世发同学的博客)
    【扩展阅读】提问的智慧(转自福州大学陈世发同学的评论)
  • 原文地址:https://www.cnblogs.com/stono/p/4860285.html
Copyright © 2011-2022 走看看