spring框架学习(四)——注解方式AOP
注解配置业务类
使用@Component("s") 注解ProductService 类
package com.how2java.service;
import org.springframework.stereotype.Component;
@Component("s")
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
注解配置切面
@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作
package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; @Aspect
@Component
public class LoggerAspect { @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
springconfig.xml
去掉原有信息,添加如下3行
<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>
扫描包com.how2java.aspect和com.how2java.service,定位业务类和切面类
<aop:aspectj-autoproxy/>
找到被注解了的切面类,进行切面配置
<?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.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>
<aop:aspectj-autoproxy/> </beans>
测试运行
package com.how2java.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.service.ProductService; public class TestSpring { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "springconfig.xml" });
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}

---------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
在how2j 网站学习spring框架时,发现有网友留了一些问题,这里我们拿出来分享一下
1. 为什么用注解方式跑程序比用xml配置要慢?
xml文件的形式,系统只需要解析xml文件中的内容,你需要自己去管理xml文件。而注解最终要调用反射,反射很耗时间的,换来的是不需要去管理xml文件,二者各有优缺点,看情况选择合适的方式。
2. 假如一个业务有两个切面如何区分先后顺序?(比较重要)
使用xml配置的时候,在定义切面时,如果通知的类型相同,可以加上order定义多个切面的执行顺序,这里有两种情况:
<aop:aspect id="logAspect2" ref="loggerAspect2" order="1">
<aop:before pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect> <aop:aspect id="logAspect3" ref="loggerAspect3" order="2">
<aop:before pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
上面的通知类型都是before,order的数值越小越先执行!!!
<aop:aspect id="logAspect2" ref="loggerAspect2" order="1">
<aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect> <aop:aspect id="logAspect3" ref="loggerAspect3" order="2">
<aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
上面的通知类型都是after,order的数值越大越先执行!!!
在定义切面时,如果通知的类型不同,则无论怎样定义order都无法决定多个切面的执行顺序
<!--该切面order="1",但通知是after类型-->
<aop:aspect id="logAspect2" ref="loggerAspect2" order="1">
<aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
<!--该切面order="2",但通知是before类型-->
<aop:aspect id="logAspect3" ref="loggerAspect3" order="2">
<aop:before pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
这种情况切面的执行顺序是按通知的类型决定的!!!
用注解的方式定义切面,执行的先后顺序其原理和xml配置一样。
3. 如何把一个切面织入两个不同的业务类中?
可在切面中加入
@Around(value = "execution(* com.java.ProductService.*(..)) || execution(* com.java.service.ProductService2.*(..))")
如果分开写,会报错,例如
@Around(value = "execution(* com.java.ProductService.*(..))"
@Around(value = "execution(* com.java.ProductService2.*(..))"
spring框架学习(四)——注解方式AOP的更多相关文章
- spring框架学习(二)——注解方式IOC/DI
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
- spring框架学习(六)AOP
AOP(Aspect-OrientedProgramming)面向方面编程,与OOP完全不同,使用AOP编程系统被分为方面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...
- Spring框架学习之注解配置与AOP思想
上篇我们介绍了Spring中有关高级依赖关系配置的内容,也可以调用任意方法的返回值作为属性注入的值,它解决了Spring配置文件的动态性不足的缺点.而本篇,我们将介绍Spring的又一大核心 ...
- spring框架学习(六)AOP事务及spring管理事务方式之Template模板
概念 1.事务 1)事务特性:ACID 原子性 :强调事务的不可分割. 一致性 :事务的执行的前后数据的完整性保持一致. 隔离性 :一个事务执行的过程中,不应该受到其他事务的干扰. 持久性 :事务一旦 ...
- Spring框架学习(9)AOP技术理解与使用
内容源自:AOP技术理解与使用 一.什么是AOP? aop技术是面向切面编程思想,作为OOP(面向对象编程)的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想. AOP底层也是 ...
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...
- [ SSH框架 ] Spring框架学习之三(AOP开发和注解的使用)
一.Spring 使用 AspectJ 进行 AOP 的开发:注解的方式 1.1 引入相关的jar包 1.2 引入spring的配置文件 <?xml version="1.0" ...
- Spring框架学习总结(上)
目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
随机推荐
- SpringBoot整合Bubbo
一.创建springboot_dubbo_provider项目 1 创建service层接口 public interface IDoSomeService { public String sayHi ...
- [PWA] Storage information for PWA application
Be careful with the storage use cases, free the storage when it is necessary.
- Oracle 对比insert和delete操作产生的undo
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/wangqingxun/article/de ...
- vue解决大文件断点续传
一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...
- 【洛谷P5158】 【模板】多项式快速插值
卡常严重,可有采用如下优化方案: 1.预处理单位根 2.少取几次模 3.复制数组时用 memcpy 4.进行多项式乘法项数少的时候直接暴力乘 5.进行多项式多点求值时如果项数小于500的话直接秦九昭展 ...
- vue transition实现页面切换效果
我们都知道vue可以做成单页应用 点击的时候就能切换 如果我们要添加一些视觉效果 比如页面切换的时候有一个缓冲效果 这个时候就需要用到vue里的transition这个标签 在使用这个标签之前需要了 ...
- Redis-5.0.5集群配置
版本:redis-5.0.5 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...
- Linux 系统管理——账号管理
一.用户账号管理 1.用户账户概述 用户账户的常见分类: 超级用户:root uid=0 gid=0 权限最大 普通用户:uid>=500 做一般权限的系统管理,权限有限. 程序用户:1 ...
- last-child为啥不生效
last-child基本定义 指定属于其父元素的最后一个子元素的 p 元素的背景色 如果你这样写是不会生效的 <div class="limit-border"> &l ...
- 安装关系型数据库MySQL 安装大数据处理框架Hadoop
作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 1.Hadoop的介绍 Hadoop最早起源于Nutch.Nut ...