使用@AspectJ注解开发Spring AOP
一、实体类:
Role
public class Role {
private int id;
private String roleName;
private String note;
@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", note='" + note + '\'' +
'}';
}
public Role() {
}
public Role(int id, String roleName, String note) {
this.id = id;
this.roleName = roleName;
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
二、连接点(join point)
1、接口:RoleService
public interface RoleService {
void printRoleInfo(Role role);
}

2、实现类:RoleServiceImp
@Component
public class RoleServiceImpl implements RoleService {
@Override
public void printRoleInfo(Role role) {
System.out.println("id = "+role.getId()+", roleName = '"+role.getId()+"', note = '"+role.getNote()+"'");
} }
三、创建切面(Aspect)
1、方式一:
@Aspect
public class RoleAspect { @Before("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
public void before() {
System.out.println("进入方法before...");
} @After("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
public void after() {
System.out.println("进入方法after...");
}
@AfterReturning("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
public void afterReturning() {
System.out.println("进入方法afterReturning...");
}
@AfterThrowing("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
public void afterThrowing() {
System.out.println("afterThrowing...");
}
}
2、方式二:切点(Pointcut)
使用注解:@Pointcut
@Aspect
public class RoleAspect {
@Pointcut("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
public void print() { } @Before("print()")
public void before() {
System.out.println("进入方法before...");
} @After("print()")
public void after() {
System.out.println("进入方法after...");
} @AfterReturning("print()")
public void afterReturning() {
System.out.println("进入方法afterReturning...");
} @AfterThrowing("print()")
public void afterThrowing() {
System.out.println("afterThrowing...");
}
}
四:配置Spring bean
方式一:采用注解java配置
@Configuration
@EnableAspectJAutoProxy//自动代理
@ComponentScan("com.wbg.springAOP.aop")
public class AopConfig {
@Bean
public RoleAspect getRoleAspct(){
return new RoleAspect();
}
}

方式二:使用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"
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">
<!--如同注解@EnableAspectJautoProxy-->
<aop:aspectj-autoproxy/>
<bean id="roleAspect" class="com.wbg.springAOP.aop.aspect.RoleAspect"/> <bean id="roleService" class="com.wbg.springAOP.aop.service.impl.RoleServiceImpl"/>
</beans>

五、测试:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AopConfig.class);
//ApplicationContext ctx = new ClassPathXmlApplicationContext("aspectj.xml");
RoleService roleService = ctx.getBean(RoleService.class);
Role role = new Role(1,"123","55");
roleService.printRoleInfo(role);
System.out.println("--------------");
//role=null;
roleService.printRoleInfo(role);
}
1、不为空:

2、为空:

六、加入环绕通知@Around
直接在切面加入:
@Around("print()")
public void around(ProceedingJoinPoint joinPoint){
System.out.println("around======before============");
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("around======after============");
}

测试:

七、给通知传递参数:&& args(..)
@Before("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..)) && args(role, sort)")

八、引入(Introduction)
目的:让Role为空时不打印
1、定义接口:RoleVerifer

2、实现接口类:RoleVeriferImpl

3、把RoleVerifier加入到切面中
com.wbg.springAOP.aop.service.impl.RoleServiceImpl+ :表示对RoleServiceImpl类进行增强
defaultImpl :代码其默认实现类,这里是RoleVerifierImpl
@DeclareParents(value = "com.wbg.springAOP.aop.service.impl.RoleServiceImpl+",defaultImpl = RoleVerifierImpl.class)
public RoleVerifier roleVerifier;

4、引入增强检查是否为空

demo:https://github.com/weibanggang/springaopstaticanddynamic
使用@AspectJ注解开发Spring AOP的更多相关文章
- spring 使用@AspectJ注解开发Spring AOP
选择切点 Spring是方法级别的AOP框架,而我们主要也是以某个类的某个方法作为切点,用动态代理的理论来说,就是要拦截哪个方法织入对应AOP通知. 代码清单:打印角色接口 package com.s ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- 基于Aspectj 注解实现 spring AOP
AOP 面向切面编程,是 OOP (面向对象编程)的补充 术语 横切关注点:方法中非主要业务逻辑部分 比如运算的模块:有验证参数.执行方法前的操作.执行方法.执行方法后的操作,验证参数.执行方法前后的 ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较
本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 基于注解的Spring AOP示例
基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...
- 基于注解的Spring AOP的配置和使用--转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- Spring注解开发系列Ⅵ --- AOP&事务
注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...
随机推荐
- spring历史和哲学
spring 历史: 2004年 Spring Framework 1.0 final 正式问世. 1.在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分 ...
- img图片加载失败默认图片
<img :src="item.goods_pic" onerror="javascript:this.src='../static/images/default. ...
- 爬虫之Beautifulsoup的基本实用
基本方法如下: # soup.a 只能找到第一个符合要求的标签 # soup.a.attrs 获取a所有的属性和属性值 # soup.a.attrs['href'] 获取href属性 # soup.a ...
- Git 命令 操作
常用 Git 命令清单 我每天使用 Git ,但是很多命令记不住.一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单. ...
- jQuery对html元素取值与赋值
以下总结了常用的jQuery选择器对html元素取值与赋值 Textbox: var str = $('#txt').val(); $('#txt').val("Set Lbl Value ...
- vs2017源文件创建代码自动版权声明注释
原来在vs2015下用的挺好,顺便移植到2017下. 用文本打开,在其头部加上 “C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterpr ...
- ArcGIS 10.1 如何连接数据库(转)
原文地址:http://blog.csdn.net/arcgis_cs/article/details/7750893 ArcGIS 10.1如何连接数据库 最近在使用ArcGIS 10.1的数据库, ...
- canvas中的非零环绕原则
非零环绕原则: 如果需要判断某一个区域是否需要填充颜色. 就从该区域中随机的选取一个点. 从这个点拉一条直线出来, 一定要拉到图形的外面. 此时以该点为圆心.看穿过拉出的直线的线段. 如果是顺时针方向 ...
- Java—IO流 对象的序列化和反序列化
序列化的基本操作 1.对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化. 2.序列化流(ObjectOutputStream),writeObject 方法用于将对象写入输出流中 ...
- Windows 如何查看本地端口被进程占用的情况?
1.查看端口占用情况: 1.开始--运行--cmd 进入命令提示符. 2. 输入netstat -ano(或netstat -nao) ,即可看到所有进程(这里显示进程的PID)占用端口的情况. 3. ...