使用@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 ...
随机推荐
- Boxlayout中button改变大小
需要先设置maximunsize neuStart.setBorder(BorderFactory.createRaisedBevelBorder()); neuStart.setMaximumSiz ...
- cocos creator Touch事件应用(触控选择多个子节点)
最近参与了cocos creator的研究,开发小游戏,结果被一个事件坑得不行不行的.现在终于解决了,分享给大家. 原理 1.触控事件是针对节点的 2.触控事件的冒泡,是一级一级往上冒泡,中间可以阻止 ...
- Struts 2 框架相比Struts1的新特性
POJO形式和POJO动作 - 已经摆脱了Struts2的动作表单的Struts框架的一个组成部分.Struts2可以使用任何的POJO接收的形式输入.同样的,你现在可以看到任何POJO的Action ...
- img图片加载失败默认图片
<img :src="item.goods_pic" onerror="javascript:this.src='../static/images/default. ...
- CSS单行、多行文本溢出显示省略号(……)解决方案
单行文本溢出显示省略号(-) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 .ellipsis{ overflow: hidden; text-overf ...
- 01.php面向对象
下面是php_oop的一些基本知识 <?php //echo "<meta charset='utf-8'>" //设置中文输出 //1.面向对象类的建立: cl ...
- <Android Framework 之路>BootAnimation(2)
前言 上一篇主要讲解了BootAnimation是从何而来,如何启动,从开机,到SurfaceFlinger服务起来,然后到执行开机动画,如果要深入的看里面的代码,是需要花一定的时间的,我们旨在了解大 ...
- ASP.NET 页面之间传递值的几种方式
1.使用QueryString, 如....?id=1; response. Redirect().... 2.使用Session变量 3.使用Server.Transfer4.Applicatio ...
- 深入理解java的形参和实参
转载声明:本文转载自公众号「码匠笔记」. 前几天在头条上看到一道经典面试题,引发了一些思考.也是写这篇文章的导火索. 背景 请看题: public classMain{ publicsta ...
- pt-mysql-summary
pt-mysql-summary主要用来输出MySQL的基本信息,可以作为数据库巡检以及刚开始熟悉数据库环境时候进行使用: [root@mxqmongodb2 bin]# ./pt-mysql-sum ...