SpringAOP增强是什么,不知道的到上一章去找,这里直接上注解实现的代码(不是纯注解,纯注解后续会有)

创建业务类代码

 @Service("dosome")//与配置文件中<bean id="dosome" class="com.com.service.DoSome"/>一样
public class DoSome {
public void dosome(){
System.out.println("我是service层");
}
//该方法为了对比最终增强和后置增强的效果,还有就是异常抛出增强的效果
public void error(){
int result=5/0;//伪造一个异常
System.out.println("我是异常方法");
}
}

创建通知类代码

 @Aspect//标注这个类是一个切面
@Component//作用与@Service作用相同
public class Advice implements AfterAdvice {
//标注一个切点,这个方法只是为了写一个表达式,什么都不用做
@Pointcut("execution(* com.cn.service.*.*(..))")
public void pointcut(){}
//前置增强
@Before("pointcut()")
public void before(JoinPoint jp){
System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
}
//后置增强
@AfterReturning(value = "pointcut()",returning = "obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
}
//环绕增强
@Around("pointcut()")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("我是环绕增强中的前置增强!");
Object proceed = jp.proceed();//植入目标方法
System.out.println("我是环绕增强中的后置增强!");
}
//异常抛出增强
@AfterThrowing(value = "pointcut()",throwing = "e")
public void error(JoinPoint jp,Exception e){
System.out.println("我是异常抛出增强"+",异常为:"+e.getMessage());
}
//最终增强
@After("pointcut()")
public void after(JoinPoint jp){
System.out.println("我是最终增强");
}
}

核心配置文件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"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--包扫描-->
<context:component-scan base-package="com.cn"/>
<!--开启ioc注解-->
<context:annotation-config/>
<!--开启aop注解-->
<aop:aspectj-autoproxy/>
</beans>

编写测试类代码

 public class App
{
public static void main( String[] args )
{
ApplicationContext ac=new ClassPathXmlApplicationContext("/applicationContext.xml");
DoSome bean = ac.getBean("dosome",DoSome.class);
bean.dosome();
//bean.error();
}
}

后续补充纯注解配置方式,更方便一些

Spring-增强方式注解实现方式的更多相关文章

  1. spring boot使用注解的方式引入mybatis的SqlSessionDaoSupport

    出现这个问题, 说明一点, 我对spring的注解方式的配置只是知道一个皮毛. 没有深入理解. 有时间要把这部分充充电 package com.zhike.qizhi.common.dao; impo ...

  2. Spring定时任务@Scheduled注解使用方式

    1.开篇 spring的@Scheduled定时任务相信大家都是十分熟悉.最近在使用过程中发现了一些问题,写篇文章,和大家分享一下.结论在最后,不想看冗长过程的小伙伴可以直接拉到最后看结论. 2.简单 ...

  3. spring定时任务的注解实现方式

    STEP 1:在spring配置文件中添加相应配置,以支持定时任务的注解实现 (一)在xml里加入task的命名空间 <!-- beans里添加:--> xmlns:task=" ...

  4. spring框架 事务 注解配置方式

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  5. Spring AOP通过注解的方式设置切面和切入点

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

  6. 【Spring】AOP注解方式实现机制

    一.概述 二.@EnableAspectJAutoProxy 注解分析 三.分析AnnotationAwareAspectJAutoProxyCreator 四.执行流程 1. registerBea ...

  7. Spring学习笔记3——使用注解的方式完成注入对象中的效果

    第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...

  8. Spring中@相关注解的意义

    1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...

  9. spring mvc 基于注解 配置默认 handlermapping

    spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...

随机推荐

  1. B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板

    You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...

  2. tensorflow 分布式训练

    TF实现分布式流程 1.创建集群 ClusterSpec & Server cluster = tf.train.ClusterSpec({"ps": ps_hosts, ...

  3. Maven - No plugin found for prefix 'tomcat7' in the current project

    问题发现: 在构建Maven项目的时候,出现了No plugin found for prefix 'tomcat7' in the current project的错误. 是需要在Maven的Pom ...

  4. 【Gson】网页上String获取的Json数据转化为对象

    1.网络上获取的String Json格式转化为对象获取数据: 需要的包:Gson Maven依赖: <!-- https://mvnrepository.com/artifact/com.go ...

  5. Luogu P3031 高于中位数

    定义序列\(x_i = f([H_i >=x])\;\;\;\;其中f(0) = -1,f(1) = 1\),那么区间[i,j]满足条件当且仅当sum_j-sum_{i-1} > 0,即s ...

  6. Hadoop的常用指令

    -help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...

  7. vscode显示当前文件完整路径信息

    Code->Preferences->Settings 搜索window.title 原本是activeEditorShort,修改 activeEditorShort => act ...

  8. 吴裕雄--天生自然ShellX学习笔记:Shell 数组

    数组中可以存放多个值.Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似). 与大部分编程语言类似,数组元素的下标由0开始. Shell 数组用括号来 ...

  9. 吴裕雄--天生自然 JAVA开发学习:StringBuffer、数组

    public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(& ...

  10. 四十三、LAMP与LNMP web架构深度优化实战-第二部

    1. 配置nginx gzip压缩功能    服务器对发出的内容进行压缩,带宽少了,体验好,速度快,但是服务端压,会使cpu使用高,压缩比高的进行压缩:文本.程序文件.数据文件.图片视频不要压缩,一般 ...