如何配置 网站二级域名,wordpress还是自己写,小蜜蜂wordpress采集,wordpress主题使用教程文章目录前言环境搭建前置业务类编写一、注解实现AOP1.编写注解实现的增强类2.在Spring配置文件中#xff0c;注册bean#xff0c;并增加支持注解的配置3.测试二、配置文件实现AOP1.编写自定义增强类2.Spring配置文件中#xff0c;注册bean#xff0c;配置增强2.测试总结前…
文章目录前言环境搭建前置业务类编写一、注解实现AOP1.编写注解实现的增强类2.在Spring配置文件中注册bean并增加支持注解的配置3.测试二、配置文件实现AOP1.编写自定义增强类2.Spring配置文件中注册bean配置增强2.测试总结前言
AOPAspect Oriented Programming称为面向切面编程在程序开发中主要用来解决一些系统层面上的问题比如日志事务权限等待。 环境搭建
1、pom.xml 配置AOP依赖 !-- AOP 依赖--dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.4/version/dependency2、Spring配置文件的命名空间中加入aop头文件
beans xmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd前置业务类编写
1、Admin的service
package com.ex.service;public interface IAdminService {public void saveAdmin(String name);
}package com.ex.service.impl;Service
public class adminServiceImpl implements IAdminService {Overridepublic void saveAdmin(String name) {System.out.println(save admin method);}
}2、user的service
package com.ex.service;public interface IUserService {public void selectUser(int id);
}package com.ex.service.impl;Service
public class userServiceImpl implements IUserService {Overridepublic void selectUser(int id) {System.out.println(user select method);}
}一、注解实现AOP
1.编写注解实现的增强类
Component
Aspect
public class LogAdvice {// springaop自动的5种aop这里全部列出// *返回类型包名*类名*方法名(..)任何参数Before(execution(* com.ex.service.impl.*.*(..)))public void before(){System.out.println(---------方法执行前before()---------);}After(execution(* com.ex.service.impl.*.*(..)))public void after(){System.out.println(---------方法执行后after()---------);}AfterReturning(execution(* com.ex.service.impl.*.*(..)))public void afterReturning(){System.out.println(---------方法返回后afterReturning()---------);}Around(execution(* com.ex.service.impl.*.*(..)))public void around(ProceedingJoinPoint jp) throws Throwable {System.out.println(-------环绕前-------);System.out.println(签名拿到方法名:jp.getSignature());//执行目标方法proceedObject proceed jp.proceed();System.out.println(-------环绕后------);System.out.println(proceed);}AfterThrowing(execution(* com.xinzhi.service.impl.*.*(..)))public void afterThrow() {System.out.println(--------------有异常发生----------------- new Date());}
}2.在Spring配置文件中注册bean并增加支持注解的配置 !-- 扫包如果使用了注解需要在开始之前去扫包--context:component-scan base-packagecom.ex/!-- aop 注解实现 配置 --aop:aspectj-autoproxy/3.测试 Testpublic void testAop(){userService.selectUser(1);System.out.println(--------------------------------);adminService.saveAdmin(aa);}结果
-------环绕前-------
签名拿到方法名:void com.ex.service.IUserService.selectUser(int)
---------方法执行前before()---------
user select method
-------环绕后------
null
---------方法执行后after()---------
---------方法返回后afterReturning()---------
--------------------------------
-------环绕前-------
签名拿到方法名:void com.ex.service.IAdminService.saveAdmin(String)
---------方法执行前before()---------
save admin method
-------环绕后------
null
---------方法执行后after()---------
---------方法返回后afterReturning()---------二、配置文件实现AOP
1.编写自定义增强类
public class MyAOP {public void before(){System.out.println(---------执行方法前打印日志--------------自定义);}public void after(){System.out.println(---------执行方法后打印日志--------------自定义);}
}2.Spring配置文件中注册bean配置增强 !--注册bean-- bean idmyAop classcom.xinzhi.aop.MyAop/!--aop的配置--aop:config!-- ref 自定义切面类 --aop:aspect refmyAOP!-- 切入点配置 --aop:pointcut idpointcut1 expressionexecution(* com.ex.service.impl.adminServiceImpl.*(..))/aop:pointcut idpointcut2 expressionexecution(* com.ex.service.impl.userServiceImpl.*(..))/!-- 织入 --aop:before pointcut-refpointcut1 methodbefore/aop:after pointcut-refpointcut2 methodafter//aop:aspect/aop:config2.测试 Testpublic void testAop2(){userService.selectUser(1);System.out.println(--------------------------------);adminService.saveAdmin(aa);}结果
user select method
---------执行方法后打印日志--------------自定义
--------------------------------
---------执行方法前打印日志--------------自定义
save admin method总结
AOP就是对指定的一批的方法在其执行过程中进行一个统一的处理将大量重复性的工作抽离了出来省事