一、实体类:

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

最新文章

  1. Learn ZYNQ (7)
  2. Ubuntu下Nutch1.2的使用
  3. python Django教程 之 安装、基本命令、视图与网站
  4. shell 循环
  5. CSS 文字阴影(text-shadow)怎么用
  6. Objective之ARC
  7. Codeforces Round #366 (Div. 2) C 模拟queue
  8. matlab函数集锦
  9. C# winform 自定义控件
  10. Android多媒体框架对音乐播放器的支持
  11. 常用的TCP选项
  12. A direct formulation for sparse PCA using semidefinite programming
  13. pkg-config 用法
  14. centos7 --kubeadm安装
  15. HTML5 &lt;li&gt; &lt;ol&gt; &lt;ul&gt; 用法
  16. Oracle数据库创建表空间
  17. 云计算设计模式(二十一)——Sharding分片模式
  18. FPGA该如何应对ASIC的大爆发?
  19. 高并发第十三弹:J.U.C 队列 SynchronousQueue.ArrayBlockingQueue.LinkedBlockingQueue.LinkedTransferQueue
  20. redis 管道技术 pipeline 简介

热门文章

  1. IDEA集成 SpringBoot+Mybaties 之 @Autowired注入报错
  2. go get 下载需要的相关工具
  3. git基本命令集合
  4. async await基本使用
  5. org.springframework.beans.factory.NoSuchBeanDefinitionException
  6. js权威指南学习笔记(二)表达式与运算符
  7. Java 文件上传与下载、email
  8. Oracle INSERT ALL 语句介绍
  9. MySQL主从复制与读写分离概念及架构分析
  10. 微信小程序开发前期准备