一、实体类:

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的更多相关文章

  1. spring 使用@AspectJ注解开发Spring AOP

    选择切点 Spring是方法级别的AOP框架,而我们主要也是以某个类的某个方法作为切点,用动态代理的理论来说,就是要拦截哪个方法织入对应AOP通知. 代码清单:打印角色接口 package com.s ...

  2. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  3. 基于Aspectj 注解实现 spring AOP

    AOP 面向切面编程,是 OOP (面向对象编程)的补充 术语 横切关注点:方法中非主要业务逻辑部分 比如运算的模块:有验证参数.执行方法前的操作.执行方法.执行方法后的操作,验证参数.执行方法前后的 ...

  4. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  5. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  6. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  7. 基于注解的Spring AOP示例

    基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...

  8. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  9. Spring注解开发系列Ⅵ --- AOP&事务

    注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...

随机推荐

  1. getElementsByName属性

    getElementsByName() 方法可返回带有指定名称的对象的集合 语法 document.getElementsByName(name) 该方法与 getElementById() 方法相似 ...

  2. Java入门到精通——调错篇之Astah Community打开报需要jre1.7运行环境

    1.问题概述     Astah Community安装完以后点击运行Astah Community的时候报此应用需要jdk1.7如下图     但是我的电脑在D盘装了jdk1.8了为什么这个软件为什 ...

  3. c++中%是什么意思?

    两种意思:1.格式化字符串输出2.整数取余 1.目前printf支持以下格式的输出,例如:printf("%c",a):输出单个字符.printf("%d",a ...

  4. Genymotion模拟器拖入文件报An error occured while deploying the file的错误

    今天需要用到资源文件,需要将资源文件拖拽到sd卡中,但老是出现这个问题: 资源文件拖不进去genymotion.查看了sd的DownLoad目录,确实没有成功拖拽进去. 遇到这种问题的,我按下面的思路 ...

  5. switch case 忘记 break

    昨天纠结了很久的问题,一直找不到原因,早上发现是一个低级错误: private void selectButton(int id) { switch (id) { case 0: recommend_ ...

  6. Qt 日志输出文件

    在Qt开发过程当中经常使用qDebug等一些输出来调试程序,但是到了正式发布的时候,都会被注释或者删除,采用日志输出来代替.     做过项目的童鞋可能都使用过日志功能,以便有异常错误能够快速跟踪.定 ...

  7. 用C读取INI配置文件

    http://blog.csdn.net/chexlong/article/details/6818017 #define CONF_FILE_PATH "Config.ini" ...

  8. dialog problem overview

    一 Datasets 1. ubuntu chat corpus: http://daviduthus.org/UCC/2. dialogue  datasets (circa 2005) 二 Tas ...

  9. PHP | 别家网站都有的登录功能,你的网站也可以有!

    如果说一个网站是一个独立的王国,那登录功能就相当于这个[王国]的大门.进出往来的人必须要通过这道[门]才能进出这个[王国],这样才能有效的达到对人流量和用户的有效监管,也可以进一步了解每个用户的喜好, ...

  10. 用java读取多种文件格式的文件(pdf,pptx,ppt,doc,docx..)

    本文通过开源pdfbox和poi进行处理多种文件格式的文本读入 1.需要的jar的maven坐标: <dependency> <groupId>org.apache.pdfbo ...