zoukankan      html  css  js  c++  java
  • javaEE之------ApectJ的切面技术===标签

    如今比較流行了aop技术之中的一个========标签

    实现步骤:

    一,导入aop标签

    方法,打开aop包。里面就有。

    这个里面就有


    然后依据选择spring的版本号。

    在配置文件里配置

    例如以下:

    <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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    这样就导入了aop标签


    二,配置切点和通知

    <aop:config>
    		<aop:aspect ref="myadvior"><!-- 须要导入作为切面的类-->
    			<aop:pointcut expression="execution(* cn..Person.*(..))" id="cut"/>
    			<aop:before method="test1" pointcut-ref="cut"/> <!-- 这是通知,拦截切点位置。也就是拦截时  须要做的事情 -->
    			<aop:aftermethod="test1" pointcut-ref="cut"/> <!-- 拦截核心之后运行的动作,所有写在test1方法里面了 -->
    			<!-- <aop:before method="test1" pointcut="execution(* cn..Person.*(..))"/> 这样也是能够的,就不用切点了,直接写在这里面 -->
    		</aop:aspect>
    	</aop:config>	
    里面的配置通知类型非常多

    三。被代理类以及自己主动注解类

    <span style="font-size:18px;"><!-- 须要的三元素 (被代理类, 自己主动代理类,切面)採用标签。切面和之前的有点不同。仅仅是一个简单的pojo导入 -->
    	<bean id="person" class="cn.aop.aspectj3.Person"></bean></span>
    <span style="font-size:18px;"> <!-- 注解自己主动标签,自己主动去查找带有注解的类和方法 -->
    <span style="white-space:pre">	</span> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </span>

    。导入我们作为切面的类

    <bean class="cn.aop.aspectj3.MyAdvisor" id="myadvior"/><!--当做切面的pojo 注入 -->

    配置文件已经完毕。


    五,被导入的作为切面的类

    public class MyAdvisor {
    
    		public void test1(){
    			System.out.println("这是test...");
    		}
    }
    
    非常普通的一类。方法名在配置切面里面,通知的时间也完毕了。。这里就能够实现想要完毕的动作了。

    标签相当于之前的,有非常大的优化,如在核心模块全然不知道是都做了拦截,进一步实现了解耦。

    =========================这里已经介绍完====================



    源码以及測试

    1,配置文件

    <?

    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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 须要的三元素 (被代理类。 自己主动代理类,切面)採用标签,切面和之前的有点不同,仅仅是一个简单的pojo导入 --> <bean id="person" class="cn.aop.aspectj3.Person"></bean> <!-- 自己主动代理注解 <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> --> <!-- 注解自己主动标签,自己主动去查找带有注解的类和方法 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <aop:config> <aop:aspect ref="myadvior"> <aop:pointcut expression="execution(* cn..Person.*(..))" id="cut"/> <aop:before method="test1" pointcut-ref="cut"/> <!-- 这是通知,拦截切点位置。也就是拦截时 须要做的事情 --> <aop:aftermethod="test1" pointcut-ref="cut"/> <!-- 拦截核心之后运行的动作。所有写在test1方法里面了 --> <!-- <aop:before method="test1" pointcut="execution(* cn..Person.*(..))"/> 这样也是能够的。就不用切点了,直接写在这里面 --> </aop:aspect> </aop:config> <bean class="cn.aop.aspectj3.MyAdvisor" id="myadvior"/><!--当做切面的pojo 注入 --> </beans>

         2,  作为切面的类

    package cn.aop.aspectj3;
    
    public class MyAdvisor {
    
    		public void test1(){
    			System.out.println("这是test...");
    		}
    }
    
    3,被代理的对象

    package cn.aop.aspectj3;
    
    public class Person {
    
    	public void say(){
    		System.out.println("...这是say..");
    	}
    	
    	public void run(){
    		System.out.println("这是person中的 run方法");
    	}
    	
    }
    
    4,測试

    @Test
    	public void Test2(){
    		ApplicationContext context =new ClassPathXmlApplicationContext("cn/aop/aspectj3/aspectj3.xml");
    		Person p =context.getBean(Person.class);
    		p.run();
    		p.say();
    	}




  • 相关阅读:
    VUE中tinymce设置字体大小、字体选择(就没有一篇文章能说的清楚的,那么我就说清楚这个问题)
    解决每次git push时需要输入用户名密码的问题
    vue路由,解决同一路由页面多次触发不刷新页面【vue开发】
    函数防抖和节流
    h5页面如何判断是系统Android,ios还是微信等
    react 结合 Promise 和 async await 解决多个异步请求 后统一设置状态问题
    对数组对象递归遍历给子对象添加父对象属性的方法
    React Hooks 使用指南
    webpack配置介绍
    React 实现键盘监听事件
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7190705.html
Copyright © 2011-2022 走看看