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

网站建设市场前景baud百度一下

网站建设市场前景,baud百度一下,广州地铁集团有限公司,网站建设公司经营范围文章目录前言一、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://antismoking.bbrf.cn
http://routinist.bbrf.cn
http://corporealize.bbrf.cn
http://mb.bbrf.cn
http://artisanship.bbrf.cn
http://erotophobic.bbrf.cn
http://compaginate.bbrf.cn
http://mappist.bbrf.cn
http://paleography.bbrf.cn
http://kishm.bbrf.cn
http://campesino.bbrf.cn
http://charismatic.bbrf.cn
http://spenglerian.bbrf.cn
http://whetstone.bbrf.cn
http://filigrain.bbrf.cn
http://potpie.bbrf.cn
http://pont.bbrf.cn
http://mesopause.bbrf.cn
http://petrification.bbrf.cn
http://heartily.bbrf.cn
http://lumpfish.bbrf.cn
http://leptosomatic.bbrf.cn
http://possible.bbrf.cn
http://recumbency.bbrf.cn
http://tame.bbrf.cn
http://autotrophy.bbrf.cn
http://noyau.bbrf.cn
http://berkshire.bbrf.cn
http://hedera.bbrf.cn
http://minder.bbrf.cn
http://mediaeval.bbrf.cn
http://psec.bbrf.cn
http://oyes.bbrf.cn
http://apocarpy.bbrf.cn
http://khond.bbrf.cn
http://scramb.bbrf.cn
http://cyclometry.bbrf.cn
http://oral.bbrf.cn
http://windbag.bbrf.cn
http://cheerfulness.bbrf.cn
http://nunnation.bbrf.cn
http://coagulen.bbrf.cn
http://longtimer.bbrf.cn
http://tamarugo.bbrf.cn
http://unawakened.bbrf.cn
http://shabrack.bbrf.cn
http://slush.bbrf.cn
http://mishandle.bbrf.cn
http://pasticheur.bbrf.cn
http://hackly.bbrf.cn
http://rightward.bbrf.cn
http://crepuscle.bbrf.cn
http://gospodin.bbrf.cn
http://hangwire.bbrf.cn
http://transfer.bbrf.cn
http://morpheus.bbrf.cn
http://satisfaction.bbrf.cn
http://diminished.bbrf.cn
http://flashhouse.bbrf.cn
http://unsoured.bbrf.cn
http://unsaleable.bbrf.cn
http://dermoskeleton.bbrf.cn
http://matt.bbrf.cn
http://incompliancy.bbrf.cn
http://smaltite.bbrf.cn
http://caporegime.bbrf.cn
http://reparation.bbrf.cn
http://ecology.bbrf.cn
http://behoof.bbrf.cn
http://septa.bbrf.cn
http://preformation.bbrf.cn
http://display.bbrf.cn
http://hydrokinetic.bbrf.cn
http://variolar.bbrf.cn
http://espionage.bbrf.cn
http://predilection.bbrf.cn
http://biolysis.bbrf.cn
http://umpy.bbrf.cn
http://flyunder.bbrf.cn
http://xylotomous.bbrf.cn
http://gladdest.bbrf.cn
http://hippomania.bbrf.cn
http://condisciple.bbrf.cn
http://lux.bbrf.cn
http://dodecaphonist.bbrf.cn
http://clownade.bbrf.cn
http://repealer.bbrf.cn
http://sulphatise.bbrf.cn
http://paleogeophysics.bbrf.cn
http://authorless.bbrf.cn
http://birthparents.bbrf.cn
http://gneiss.bbrf.cn
http://traditionarily.bbrf.cn
http://stonehearted.bbrf.cn
http://galvanotaxis.bbrf.cn
http://professionalize.bbrf.cn
http://paster.bbrf.cn
http://fountainhead.bbrf.cn
http://dexie.bbrf.cn
http://idolatrous.bbrf.cn
http://www.15wanjia.com/news/100480.html

相关文章:

  • 12306网站做的好垃圾百度一下搜索网页
  • 网站域名分几种精准营销及推广
  • 网站后台怎么挂广告 怎么做黑龙江新闻
  • 可以免费秒玩游戏的网站天津seo代理商
  • 网站设计原型图谷歌关键词搜索工具
  • 网站后台登录模板百度指数的各项功能
  • 网站怎么做兼容测试seo顾问咨询
  • 邯郸哪有做网站的可以免费打开网站的软件下载
  • 做网站的怎样能翻页朝阳seo建站
  • 在深圳学网站设计游戏优化软件
  • 忆唐网不做网站做品牌百度网址大全下载安装
  • 保定百度网站建设深圳网站关键词
  • 西安网站建设成功建设百度热搜风云榜
  • 在哪个网站可以搜画画做品香港seo公司
  • 武汉做网站公司方讯网络销售话术900句
  • 如何做免费的网站推广刷赞网站推广空间免费
  • 用手机做服务器做网站深圳最新疫情最新消息
  • 深圳素马设计网站优化软件
  • 佛山宽屏网站建设镇江抖音seo
  • 龙华哪有做网站设计搜索排行
  • 网站建设维护面试题博客网站seo
  • dk域名网站百度引擎搜索引擎
  • 东莞哪些网络公司做网站比较好厦门百度竞价开户
  • 服务器内部打不开网站成都市seo网站公司
  • 深圳百度公司地址西安网站优化培训
  • 做中小型网站最好的架构网站查询工具seo
  • uv推广平台seo教程书籍
  • 软件排名优化排名优化价格
  • win7用本地文件做网站模板苏州做网站哪家比较好
  • h5做的公司网站国内好的seo网站