ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring(xml方法的五种通知)

2021-10-25 17:33:17  阅读:152  来源: 互联网

标签:xml Spring void aop say 五种 通知 import public


Spring-AOP

什么是切面?

首先理解什么是切点:就是我们要作用的那个方法。

接着理解什么是通知:通知就是在方法之前,方法之后进行额外业务。

最后理解什么是织入:织入就是将切点和通知进行联系。

切面就是这三概念的总和

理解什么是aop

例如静态代理一样,在使用方法之前做一个通知。

aop原理就是动态代理。

前置通知:before

使用前记得导入包aop的包。

image

我们先实现一个类

package daoimpl;
//定义一个say类
public class Say {
    //定义一个说出一句话的方法SayAWord()
	public String SayAWord() {
		return "你好aop";
	}
}

再定义一个people类

package serviceimpl;
import daoimpl.Say;
public class People {
//人具备说话的属性
private Say say;
   
public String PeopleSay() {
	   return this.say.SayAWord();
   }
public Say getSay() {
	return say;
}

public void setSay(Say say) {
	this.say = say;
}   
}

建立applicationContent.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:p="http://www.springframework.org/schema/p"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd ">
     <!--注册say类  -->                                                        
     <bean id="say" class="daoimpl.Say"></bean>                                            <!--注册people类  -->             
     <bean id="people" class="serviceimpl.People" autowire="byName"></bean>                                                         

     
</beans>

建立测试类,实例化一个people

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import serviceimpl.People;

public class TestBefore {
public static void main(String[] args) {
	ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContent.xml");
	People people=(People)applicationContext.getBean("people");
	people.PeopleSay();
}
}

image

现在已经可以建立一个人说话的例子了。

接下来在实现前置通知before

建立一个aop包,在里面建立UseServiceLog类

image

package aop;


import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
public class UseServiceLog {
  private Logger logger=Logger.getLogger(UseServiceLog.class);
  //前置通知的方法
  public void before(JoinPoint p) {
	 logger.info("调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法"); 
  }
  
}

这个before就是调用的前置方法

为applicationContent.xml多添加下面的配置

 <!-- 把before引入 -->
   <bean id="UseServiceLog" class="aop.UseServiceLog"></bean>   
   <!--开启aop-->
   <aop:config>
     <!-- 确定切点(匹配方法名字叫public String PeopleSay()的方法) -->
     <aop:pointcut expression="execution(public String PeopleSay())" id="pointcut"></aop:pointcut>
     
     <!--对这个方法织入也就是坐切点和before关联  -->
     <aop:aspect ref="UseServiceLog">
        <aop:before method="before" pointcut-ref="pointcut"></aop:before>
     </aop:aspect>
   </aop:config>

控制台输出结果
image

后置通知:after

类似于前置通知

public void after(JoinPoint p) {
	  logger.info("-----调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法");
  }
<aop:config>
     <!-- 确定切点(匹配方法名字叫public String PeopleSay()的方法) -->
     <aop:pointcut expression="execution(public String PeopleSay())" id="pointcut"></aop:pointcut>
     
     <!--对这个方法织入也就是坐切点和before关联  -->
     <aop:aspect ref="UseServiceLog">
        <aop:before method="before" pointcut-ref="pointcut"></aop:before>
        <!--后置就是前置换了一个单词  -->
        <aop:after method="after" pointcut-ref="pointcut"/>
     </aop:aspect>
   </aop:config>

返回通知:afterruntime

 
  public void afterreturn(JoinPoint p,Object result) {
	  logger.info("-----调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法"
			  +result.toString()
			  );
	  System.out.println("返回通知");
  }
<!--开启aop-->
   <aop:config>
     <!-- 确定切点(匹配方法名字叫public String PeopleSay()的方法) -->
     <aop:pointcut expression="execution(public String PeopleSay())" id="pointcut"></aop:pointcut>
     
     <!--对这个方法织入也就是坐切点和before关联  -->
     <aop:aspect ref="UseServiceLog">
        <aop:before method="before" pointcut-ref="pointcut"></aop:before>
     
        <aop:after method="after" pointcut-ref="pointcut"/>
        <!--返回通知  -->
        <aop:after-returning method="afterreturn" pointcut-ref="pointcut" returning="result"/>
     </aop:aspect>
   </aop:config>

标签:xml,Spring,void,aop,say,五种,通知,import,public
来源: https://www.cnblogs.com/SweetheartAndPeaches/p/15459615.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有