当前位置: 首页 > news >正文

外贸仿牌网站建设清远今日头条最新消息

外贸仿牌网站建设,清远今日头条最新消息,拍卖网站建设公司,企业网站建设教程 pdf文章目录前言一、AOP的底层实现原理二、AOP的两种开发模式1.使用xml配置文件1.1 添加AOP依赖1.2 创建UserService1.3创建UserServiceImpl1.4创建通知类1.5 创建applicationContext.xml(添加aop约束)1.6 测试2.使用注解开发2.1 创建bean.xml文件配置注解方…

文章目录

  • 前言
  • 一、AOP的底层实现原理
  • 二、AOP的两种开发模式
    • 1.使用xml配置文件
      • 1.1 添加AOP依赖
      • 1.2 创建UserService
      • 1.3创建UserServiceImpl
      • 1.4创建通知类
      • 1.5 创建applicationContext.xml(添加aop约束)
      • 1.6 测试
    • 2.使用注解开发
      • 2.1 创建bean.xml文件配置注解方式
      • 2.2 在通知类上使用相关注解
      • 2.3 测试
  • 总结


前言

spring的核心是IOC(控制反转)和AOP(面向切面编程)。AOP面向切面编程是通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。


一、AOP的底层实现原理

此处再解释,详情请看我这篇文章
静态代理和动态代理https://blog.csdn.net/l_zl2021/article/details/127095878

二、AOP的两种开发模式

关于AOP的案例,看我这边篇文章,本文只是记录两种AOP开发方式
AOP初识https://blog.csdn.net/l_zl2021/article/details/127113425

1.使用xml配置文件

1.1 添加AOP依赖

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.20</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.9.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>compile</scope></dependency></dependencies>

1.2 创建UserService

package com.wmj.service;//目标对象target
public interface UserService {//未增强的方法叫做连接点JoinPoint//已增强的方法叫做切入点PointCutpublic void add();public void delete();
}

1.3创建UserServiceImpl

代码如下(示例):

package com.wmj.service.impl;import com.wmj.service.UserService;public class UserServiceImpl implements UserService {@Overridepublic void add() {System.out.println("添加用户..");//int i = 1/0;}@Overridepublic void delete() {System.out.println("删除用户..");}
}

1.4创建通知类

前置通知(before):目标方法运行之前调用
后置通知(after-returning):在目标方法运行之后调用 (如果出现异常不会调用)
环绕通知(around):在目标方法之前和之后都调用(ProceedingJoinPoint对象 -->> 调用proceed方法)
异常拦截通知(after-throwing):如果出现异常,就会调用
最终通知(after):在目标方法运行之后调用 (无论是否出现 异常都会调用)

package com.wmj.advice;import org.aspectj.lang.ProceedingJoinPoint;//通知类,增强的代码(方法)Advice
public class MyAdvice {public void before(){System.out.println("前置通知,目标对象调用方法前执行");}public void after(){System.out.println("后置通知(最终通知),目标对象调用方法后执行,无论是否发生异常都执行");}public void after_returning(){System.out.println("后置通知,目标对象调用方法后执行,发生异常不执行");}public void after_throwing(){System.out.println("异常通知,发生异常执行");}public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("环绕通知,目标对象调用方法之前");joinPoint.proceed();System.out.println("环绕通知,目标对象调用方法之后");}}

1.5 创建applicationContext.xml(添加aop约束)

<?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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --><bean id="userService" class="com.wmj.service.impl.UserServiceImpl"></bean><!-- 通知 --><bean id="myAdvice" class="com.wmj.advice.MyAdvice"></bean><!-- aop --><!-- 默认使用JDK动态代理 --><!-- proxy-target-class="true" 使用cglib --><aop:config proxy-target-class="true"><!-- 配置切入点 切入点表达式的写法:execution(表达式)public void com.abyg.service.UserServiceImpl.save() void com.wmj.service.UserServiceImpl.save()  其他修饰符无返回值的save空参方法* com.wmj.service.UserServiceImpl.save()  有或者无返回值的save空参方法* com.wmj.service.UserServiceImpl.*()  有或者无返回值的所有空参方法* com.wmj.service.*ServiceImpl.*(..)  有或者无返回值的所有有参或者空参方法* com.wmj.service..*ServiceImpl.*(..)  一般不用,service包下的子包和孙包以ServiceImpl结尾的类中的方法
--><!-- 切入点 -->
<!--                <aop:pointcut id="pc" expression="execution(public void com.wmj.service.impl.UserServiceImpl.add())"/>--><aop:pointcut id="pc" expression="execution(* com.wmj.service.impl.*ServiceImpl.*(..))"/><!-- 切面 --><aop:aspect ref="myAdvice"><!-- 配置前置通知对应的方法 --><aop:before method="before" pointcut-ref="pc"></aop:before><!-- 配置后置通知(最终通知)对应的方法 --><aop:after method="after" pointcut-ref="pc"></aop:after><!-- 配置后置通知对应的方法,发生异常不执行 --><aop:after-returning method="after_returning" pointcut-ref="pc"></aop:after-returning><!-- 配置异常通知对应的方法,发生异常执行 --><aop:after-throwing method="after_throwing" pointcut-ref="pc"></aop:after-throwing><!-- 配置环绕通知对应的方法 --><aop:around method="around" pointcut-ref="pc"></aop:around></aop:aspect></aop:config></beans>

1.6 测试

package com.wmj.test;import com.wmj.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {@Testpublic void testUserService(){ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService)applicationContext.getBean("userService");userService.add();userService.delete();}}

2.使用注解开发

2.1 创建bean.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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --><!-- 准备工作: 导入aop(约束)命名空间 --><!-- 1.配置目标对象 --><bean id="userService" class="com.wmj.service.impl.UserServiceImpl"></bean><!-- 2.配置通知对象 --><bean id="myAdvice" class="com.wmj.advice.MyAdvice"></bean><!-- 3.开启使用注解完成织入 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

2.2 在通知类上使用相关注解

@Aspect
//表示该类是一个通知类
//通知类,增强的代码(方法)Advice
public class MyAdvice {//自己设置一个切点,管理重复代码@Pointcut("execution(* com.wmj.service.impl.*ServiceImpl.*(..))")public void pc(){}//前置通知//指定该方法是前置通知,并制定切入点@Before("MyAdvice.pc()")public void before(){System.out.println("前置通知,目标对象调用方法前执行");}//最终通知@After("execution(* com.wmj.service.impl.*ServiceImpl.*(..))")public void after(){System.out.println("后置通知(最终通知),目标对象调用方法后执行,无论是否发生异常都执行");}//后置通知@AfterReturning("execution(* com.wmj.service.impl.*ServiceImpl.*(..))")public void after_returning(){System.out.println("后置通知,目标对象调用方法后执行,发生异常不执行");}//异常通知@AfterThrowing("execution(* com.wmj.service.impl.*ServiceImpl.*(..))")public void after_throwing(){System.out.println("异常通知,发生异常执行");}//环绕通知@Around("execution(* com.wmj.service.impl.*ServiceImpl.*(..))")public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("环绕通知,目标对象调用方法之前");joinPoint.proceed();System.out.println("环绕通知,目标对象调用方法之后");}}

2.3 测试

@Test
public void testUserService(){ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("bean.xml");UserService userService = (UserService)applicationContext.getBean("userService");userService.add();userService.delete();
}

总结

本文记录了两种开发AOP编程的方式

http://www.ds6.com.cn/news/98836.html

相关文章:

  • b2c电子商务网站系统下载深圳seo优化推广
  • 网页制作与网站建设策划书案例百度 站长工具
  • 代挂网站维护seo外包优化网站
  • 网站怎么做跳转页面销售培训课程
  • 制作做网站的基本流程seo云优化
  • 厦门八优网站建设网络营销的策划方案
  • 河南做网站联系电话今天最新新闻事件报道
  • 网站建设 唐山廊坊seo排名霸屏
  • 如何生成自己的网站seo网站优化怎么做
  • 如何做登录网站申请自媒体平台注册
  • 电商运营网站 建设企业百度推广怎么收费
  • 做足球预测的网站seo网站关键词排名优化公司
  • 上海微信网站公司哪家好编程培训班学费一般多少钱
  • 南宁网站建设团队推广公司是做什么的
  • 谷歌网站的主要内容关键词查网址
  • 腾讯云网站备案如何快速提升自己
  • 平湖网站建设公司克电商运营助理
  • 网站开发+.net+开源桂林seo
  • 中牟高端网站建设网络营销案例及分析
  • 凡客诚品正品男宁波seo哪家好
  • 广州做公司网站的公司有哪些seo是指什么
  • 外贸公司网站建设费的会计科目合肥网站优化推广方案
  • 买网站空间需要知道的省委副书记
  • 广东哪家网站建设太原免费网站建站模板
  • 绵阳市做公司网站百度收录查询api
  • 莆田做外贸网站鄂州网站seo
  • 镇江网站设计建设百度排名优化软件
  • 韶关建网站seo搜索优化网站推广排名
  • web制作网页盒子搜索引擎优化seo方案
  • 泰安市建设信息网站做广告的怎么找客户