0基础建站网站搭建教程,如何做宣传推广的网站链接,级a做爰片免费视网站,怀宁县住房和城乡建设局网站目录
一、引入AOP
二、核心AOP概念和术语
三、切点表达式
四、Spring实现AOP
#xff08;一#xff09;AspectJ的支持
1 、基于注解开发
1.1 引入依赖
1.2 实现目标类
1.3 定义切面类#xff08;日志管理#xff09;
1.4 将目标类和切面类纳入Spring容器
1.5 开…目录
一、引入AOP
二、核心AOP概念和术语
三、切点表达式
四、Spring实现AOP
一AspectJ的支持
1 、基于注解开发
1.1 引入依赖
1.2 实现目标类
1.3 定义切面类日志管理
1.4 将目标类和切面类纳入Spring容器
1.5 开启组件扫描、自动代理
1.6 测试
2、基于XML方式开发
(二 基于Schema的AOP支持
1.1 声明Aspect
1.2 编写Spring配置文件(目标类使用组件扫描)
1.3 测试
五、总结 AOP Aspect Oriented Programming意为面向切面编程通过预编译和运行期间动态代理实现程序功能的一种技术。Spring中通过JDK动态代理和Cglib动态代理技术实现AOP。Spring可以灵活切换这两种动态代理方式如果代理接口会默认使用JDK动态代理如果代理某个类这个类没有实现接口就会切换Cglib。此外我们也可以配置Spring选择。 一、引入AOP 系统开发中不仅要做核心业务 还需要做一些系统业务例如事务管理、运行记录、安全日志等这些系统业务被称为交叉业务。 如果在每一个业务处理过程当中都掺杂这些交叉业务进去的话存在两方面问题
交叉业务代码在多个业务流程中反复出现显然这些交叉代码没有得到复用。并且修改这些交叉代码修改起来很麻烦。程序员无法专注核心业务代码的编写在编写核心代码的同时还需要处理这些交叉业务。 见上图方便理解AOP思想。将非核心业务与核心业务进行分离同时把这些非核心业务切入到业务流程当中的过程称为AOP。
二、核心AOP概念和术语
JoinPoint连接点程序执行过程中的一个点例如一个方法的执行或一个异常的处理。在Spring AOP中一个连接点总是代表一个方法的执行。切点Pointcut真正切入切面的方法指定哪些方法需要切入 通知Advice一个切面在特定连接点采取的行动根据通知类型确定执行流程。通知包括前置通知、后置通知、环绕通知等。Aspect切面通知 切面 Target object目标对象 被一个或多个切面所 advice 的对象。也被称为 advised object。由于Spring AOP是通过使用运行时代理来实现的这个对象总是一个被代理的对象。 AOP proxy代理对象 一个由AOP框架创建的对象以实现切面契约advice 方法执行等。在Spring框架中AOP代理是一个JDK动态代理或CGLIB代理。 Weaving织入将通知应用到目标对象的过程。
三、切点表达式 切点表达式用来定义通知往哪些方法切入。
切点表达式语法格式 execution( [访问权限修饰符] 返回值类型 [全限定类名] 方法名形式参数列表[异常] ) 构成 Pointcut 注解的值的 pointcut 表达式是一个常规的AspectJ pointcut表达式。关于AspectJ的 pointcut 语言的全面讨论请参见 《AspectJ编程指南》以及 AspectJ 5开发者笔记 的扩展或关于AspectJ的书籍之一如Colyer等人的《Eclipse AspectJ》或Ramnivas Laddad的《AspectJ in Action》。
四、Spring实现AOP
一AspectJ的支持
1 、基于注解开发
1.1 引入依赖
!--spring context依赖--
dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.1.9.RELEASE/version
/dependency
!--spring aop依赖--
dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion5.1.9.RELEASE/version
/dependency
!--spring aspects依赖--
dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion5.1.9.RELEASE/version
/dependency
1.2 实现目标类
Service(userService)
public class UserServiceImpl implements UserService {Autowiredprivate UserMongoDao userMongoDao;Overridepublic User login(String name, String password) {Document doc new Document();doc.put(name, name);doc.put(password, password);ArrayListDocument list userMongoDao.queryAll(doc);User user null;if (! list.isEmpty()) {Document document list.get(0);user new User();user.setId((ObjectId) document.get(_id));user.setName((String) document.get(name));}return user;}
}
1.3 定义切面类日志管理
package com.zookin.service.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;Component
Aspect
public class LoggerAspect {private static final Logger LOGGER LoggerFactory.getLogger(LoggerAspect.class);Pointcut(execution(public * com.zookin.service.*.*(..)))public void logPointcut() {}Before(logPointcut())public void beforeAdvice(JoinPoint joinPoint) {LOGGER.info(before advice: joinPoint.getSignature().getName());}Around(logPointcut())public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {long startTime System.currentTimeMillis();Object result joinPoint.proceed();long endTime System.currentTimeMillis();LOGGER.info(Method: joinPoint.getSignature().getName() execution time: (endTime - startTime) ms);return result;}AfterReturning(pointcut logPointcut(), returning result)public void afterReturningAdvice(JoinPoint joinPoint, Object result) {LOGGER.info(After returning from method: joinPoint.getSignature().getName() , result: result);}AfterThrowing(pointcut logPointcut(), throwing exception)public void afterThrowingAdvice(JoinPoint joinPoint, Exception exception) {LOGGER.error(Exception thrown from method: joinPoint.getSignature().getName() , exception: exception.getMessage());}}1.4 将目标类和切面类纳入Spring容器 目标类添加Service注解切面类添加Component、Aspect注解。
1.5 开启组件扫描、自动代理
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdcontext:component-scan base-packagecom.zookin.service/aop:aspectj-autoproxy proxy-target-classtrue//beans
1.6 测试
Testpublic void loginTest() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);UserService userService applicationContext.getBean(userService, UserService.class);User login userService.login(zookin, zookin);System.out.println(login);
} 2、基于XML方式开发
略....
(二 基于Schema的AOP支持 如果喜欢基于XML的格式Spring也提供了对使用 aop 命名空间标签定义切面的支持。它支持与使用 AspectJ 风格时完全相同的 pointcut 表达式和advice种类。要使用本节描述的 aop 命名空间标签你需要导入 spring-aop schema如 基于XML schema的配置 中所述。 在你的Spring配置中所有的aspect和 advisor元素都必须放在一个 aop:config 元素你可以在一个应用上下文配置中拥有多个 aop:config 元素。一个 aop:config 元素可以包含 pointcut、 advisor 和 aspect 元素注意这些必须按顺序声明。 1.1 声明Aspect 通过使用 aop:aspect 元素来声明一个切面并通过使用 ref 属性来引用支持 Bean如下例所示。
aop:configaop:aspect idmyAspect refaBean.../aop:aspect
/aop:configbean idaBean class......
/bean
支持切面的Bean本例中的 aBean当然可以像其他Spring Bean一样被配置和依赖注入。
1.2 编写Spring配置文件(目标类使用组件扫描)
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdcontext:component-scan base-packagecom.zhj.service/aop:aspectj-autoproxy proxy-target-classtrue/bean idtimerAspect classcom.zhj.service.aspect.TimerAspect/beanaop:configaop:pointcut idaa expressionexecution(* com.zhj.service.CommodityService.*(..))/aop:aspect reftimerAspectaop:around methodtime pointcut-refaa//aop:aspect/aop:config/beans
1.3 测试 Testpublic void commodityTest() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);CommodityService commodityService applicationContext.getBean(commodityService, CommodityService.class);ArrayListCommodity commodities commodityService.queryCommodity(自行车, 0, null);} 五、总结 AOP是Spring框架的核心组件AOP蕴含的底层原理十分值得了解和挖掘。Spring 框架自5.0版本以来集成了Log4j、SLF4j框架本文我简单地整合日志框架来说明AOP的实现此外我们可以使用AOP实现事务管理见上一篇博客。上述内容如果有错误的地方希望大佬们可以指正。我一直在学习的路上您的帮助使我收获更大觉得对您有帮助的话还请点赞支持我也会不断更新文章Spring-事务支持https://mp.csdn.net/mp_blog/creation/editor/134687195