SpingAOP案例(一)注解(Annotation)方式

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/20698675
有些类请参照上一篇文章的其它类,这里就不列举了
//这是切面类
package com.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class TestAnnotationAspect {

	
	@Pointcut("execution(* com.spring.service.*.*(..))")
    @SuppressWarnings("unused")
	private void pointCutMethod() {

		
    }  
	
	@Before("pointCutMethod()")
	 public void doBefore() {  
	        System.out.println("前置通知");  
	    }  
	 
	 //声明后置通知  
    @AfterReturning(pointcut = "pointCutMethod()", returning = "result")  
	 public void doAfterReturning(String result) {  
	        System.out.println("后置通知");  
	        result="";
	        System.out.println("---" + result + "---");  
	    }  
	 
	 //声明例外通知  
	    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")  
	    public void doAfterThrowing(Exception e) {  
	        System.out.println("例外通知");  
	        System.out.println(e.getMessage());  
	    }  
	    //声明环绕通知  
	    @Around("pointCutMethod()")  
	    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
	        System.out.println("进入方法---环绕通知");  
	        Object o = pjp.proceed();  
	        System.out.println("退出方法---环绕通知");  
	        return o;  
	    }  
}
//这是配置文件
<?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:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
        
       <!--   xml方式
    <aop:config>  
        <aop:aspect id="TestAspect" ref="aspectBean">  
           
            <aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))" />  
            <aop:before pointcut-ref="businessService" method="doBefore"/>  
            <aop:after pointcut-ref="businessService" method="doAfter"/>  
            <aop:around pointcut-ref="businessService" method="doAround"/>  
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
        </aop:aspect>  
    </aop:config>  
      
    <bean id="aspectBean" class="com.spring.aop.TestAspect" />  
    <bean id="aService" class="com.spring.service.AServiceImpl" ></bean>  
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
    -->
    
     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />  
  
    <bean id="aspectBean" class="com.spring.aop.TestAnnotationAspect" />  
    <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean> 
</beans>  


 
 
相关文章
|
网络协议
使用netwox/TCP协议检测网络性能
使用netwox/TCP协议检测网络性能
293 0
|
Java 编译器 Go
Go 语言 入门 && 基于 GoLand 2023.1 创建第一个Go程序
Go 语言 入门 && 基于 GoLand 2023.1 创建第一个Go程序
404 0
|
关系型数据库 MySQL 数据安全/隐私保护
使用 Docker 部署一个文档管理系统,让宝贵文档不在丢失!
使用 Docker 部署一个文档管理系统,让宝贵文档不在丢失!
|
机器学习/深度学习 PyTorch 算法框架/工具
通过实例学习Pytorch加载权重.load_state_dict()与保存权重.save()
通过实例学习Pytorch加载权重.load_state_dict()与保存权重.save()
264 0
|
数据可视化 Python
【PyQt5桌面应用开发】2.可视化界面编程步骤(超级详细)
【PyQt5桌面应用开发】2.可视化界面编程步骤(超级详细)
1593 1
|
机器学习/深度学习 存储 数据采集
m基于强化学习的永磁同步电机位置控制器simulink仿真,对比传统的PI控制器和模糊PI控制器
m基于强化学习的永磁同步电机位置控制器simulink仿真,对比传统的PI控制器和模糊PI控制器
581 1
|
人工智能 自然语言处理 算法
AIGC创作的一些思路和方法
AIGC创作的一些思路和方法
468 2
|
前端开发 JavaScript Go
Python3+Flask结合Socket.io配合前端Vue实现简单全双工在线客服系统
在之前的一篇文章中:[为美多商城(Django2.0.4)添加基于websocket的实时通信,主动推送,聊天室及客服系统](https://v3u.cn/a_id_67),详细介绍了websocket协议以及结合Django如何实现各种功能,本次我们尝试采用Socket.io这个库和Flask来配合使用,完成一个简易的在线客服聊天系统,看看二者有什么区别。
Python3+Flask结合Socket.io配合前端Vue实现简单全双工在线客服系统
|
网络协议 Java 应用服务中间件
【高并发】高并发环境下如何优化Tomcat性能?看完我懂了!
Tomcat作为最常用的Java Web服务器,随着并发量越来越高,Tomcat的性能会急剧下降,那有没有什么方法来优化Tomcat在高并发环境下的性能呢?
670 1
【高并发】高并发环境下如何优化Tomcat性能?看完我懂了!