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

设计一个app软件多少钱郑州网站建设专业乐云seo

设计一个app软件多少钱,郑州网站建设专业乐云seo,广东东莞工厂,日本WordPress主机文章目录 一、日志处理二、事务控制三、参数校验四、自定义注解五、AOP 方法失效问题1. ApplicationContext2. AopContext3. 注入自身 六、附录1. 示例代码 AOP 提供了一种面向切面操作的扩展机制,通常这些操作是与业务无关的,在实际应用中,可…

文章目录

    • 一、日志处理
    • 二、事务控制
    • 三、参数校验
    • 四、自定义注解
    • 五、AOP 方法失效问题
      • 1. ApplicationContext
      • 2. AopContext
      • 3. 注入自身
    • 六、附录
      • 1. 示例代码

AOP 提供了一种面向切面操作的扩展机制,通常这些操作是与业务无关的,在实际应用中,可以实现:日志处理、事务控制、参数校验和自定义注解等功能。

Spring AOP 的原理参阅:《Spring中的AOP和动态代理》

一、日志处理

在调试程序时,如果需要在执行方法前打印方法参数,或者在执行方法后打印方法返回结果,可以使用切面来实现。

@Slf4j
@Aspect
@Component
public class LoggerAspect {@Around("execution(* cn.codeartist.spring.aop.sample.*.*(..))")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {// 方法执行前日志log.info("Method args: {}", joinPoint.getArgs());Object proceed = joinPoint.proceed();// 方法执行后日志log.info("Method result: {}", proceed);return proceed;}
}

二、事务控制

Spring 提供的声明式事务也是基于 AOP 来实现的,在需要添加事务的方法上面使用 @Transactional 注解。

@Service
public class DemoService {@Transactional(rollbackFor = Exception.class)public void insertBatch() {// 带事务控制的业务操作}
}

三、参数校验

如果需要在方法执行前对方法参数进行校验时,可以使用前置通知来获取切入点方法的参数,然后进行校验。

@Slf4j
@Aspect
@Component
public class ValidatorAspect {@Before("execution(* cn.codeartist.spring.aop.sample.*.*(..))")public void doBefore(JoinPoint joinPoint) {// 方法执行前校验参数Object[] args = joinPoint.getArgs();}
}

四、自定义注解

因为 AOP 可以拦截到切入点方法,Spring 也支持通过注解的方式来定义切点表达式,所以可以通过 AOP 来实现自定义注解的功能。

例如,自定义一个注解来实现声明式缓存,把方法的返回值进行缓存。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cacheable {/*** 缓的Key*/String key();/*** 缓存过期时间*/long timeout() default 0L;/*** 缓存过期时间单位(默认:毫秒)*/TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
}

然后定义一个切片来实现常规的缓存操作,先读缓存,缓存不存在时执行方法,然后把方法的返回结果进行缓存。

@Aspect
@Component
public class AnnotationAspect {@Around("@annotation(cacheable)")public Object doAround(ProceedingJoinPoint joinPoint, Cacheable cacheable) throws Throwable {// 自定义缓存逻辑return joinPoint.proceed();}
}

五、AOP 方法失效问题

Spring AOP 的原理是在原有方法外面增加一层代理,所以在当前类调用 AOP 方法时,因为 this 指向的是当前对象,而不是代理对象,所以 AOP 会失效。

@Service
public class DemoService {public void insert() {// 该方法事务会失效insertBatch();}@Transactional(rollbackFor = Exception.class)public void insertBatch() {// 带事务控制的业务操作}
}

解决这个问题的常用方法有下面三种:

1. ApplicationContext

使用 ApplicationContext 来手动获取 Bean 对象,来调用 AOP 方法:

@Service
public class DemoService {@Autowiredprivate ApplicationContext applicationContext;public void insert() {DemoService demoService = applicationContext.getBean(DemoService.class);demoService.insertBatch();}@Transactional(rollbackFor = Exception.class)public void insertBatch() {// 带事务控制的业务操作}
}

2. AopContext

使用 AopContext 工具类来获取当前对象的代理对象。

@Service
public class DemoService {public void insert() {((DemoService) AopContext.currentProxy()).insertBatch();}@Transactional(rollbackFor = Exception.class)public void insertBatch() {// 带事务控制的业务操作}
}

3. 注入自身

使用 Spring 注入自身来调用 AOP 方法:

@Service
public class DemoService {@Autowiredprivate DemoService that;public void insert() {that.insertBatch();}@Transactional(rollbackFor = Exception.class)public void insertBatch() {// 带事务控制的业务操作}
}

六、附录

1. 示例代码

Gitee 仓库:https://gitee.com/code_artist/spring

在这里插入图片描述


文章转载自:
http://choreodrama.nLcw.cn
http://pudendum.nLcw.cn
http://foss.nLcw.cn
http://oldish.nLcw.cn
http://sclerogenous.nLcw.cn
http://outswing.nLcw.cn
http://tideway.nLcw.cn
http://concession.nLcw.cn
http://sillily.nLcw.cn
http://botulinus.nLcw.cn
http://distraint.nLcw.cn
http://inaugurator.nLcw.cn
http://smotheration.nLcw.cn
http://sidestroke.nLcw.cn
http://epigone.nLcw.cn
http://piscator.nLcw.cn
http://voile.nLcw.cn
http://accelerometer.nLcw.cn
http://outspent.nLcw.cn
http://unturned.nLcw.cn
http://xql.nLcw.cn
http://geneticist.nLcw.cn
http://adventurist.nLcw.cn
http://grammy.nLcw.cn
http://unwell.nLcw.cn
http://yamasee.nLcw.cn
http://chthonian.nLcw.cn
http://esteem.nLcw.cn
http://yawl.nLcw.cn
http://unlicensed.nLcw.cn
http://lp.nLcw.cn
http://confessed.nLcw.cn
http://programme.nLcw.cn
http://riprap.nLcw.cn
http://aboardage.nLcw.cn
http://richly.nLcw.cn
http://claypan.nLcw.cn
http://inkblot.nLcw.cn
http://ebulliometer.nLcw.cn
http://tenter.nLcw.cn
http://brother.nLcw.cn
http://melilot.nLcw.cn
http://eudemonic.nLcw.cn
http://shooter.nLcw.cn
http://distractingly.nLcw.cn
http://removalist.nLcw.cn
http://indeterminate.nLcw.cn
http://dogfish.nLcw.cn
http://decruit.nLcw.cn
http://hapaxanthous.nLcw.cn
http://crusado.nLcw.cn
http://astronome.nLcw.cn
http://historiography.nLcw.cn
http://habakkuk.nLcw.cn
http://joskin.nLcw.cn
http://dishallow.nLcw.cn
http://uncloister.nLcw.cn
http://reglet.nLcw.cn
http://motherlike.nLcw.cn
http://contemporary.nLcw.cn
http://supertransuranic.nLcw.cn
http://beestings.nLcw.cn
http://psephology.nLcw.cn
http://wifelike.nLcw.cn
http://automata.nLcw.cn
http://orthophosphate.nLcw.cn
http://velikovskianism.nLcw.cn
http://pba.nLcw.cn
http://polycotyledony.nLcw.cn
http://quoteworthy.nLcw.cn
http://refasten.nLcw.cn
http://mizenyard.nLcw.cn
http://denicotinize.nLcw.cn
http://thanksgiver.nLcw.cn
http://gardner.nLcw.cn
http://epural.nLcw.cn
http://popped.nLcw.cn
http://kinsoku.nLcw.cn
http://uraniscus.nLcw.cn
http://sentimo.nLcw.cn
http://nanoprogramming.nLcw.cn
http://ru.nLcw.cn
http://sovietology.nLcw.cn
http://postfigurative.nLcw.cn
http://imperturbability.nLcw.cn
http://blackbody.nLcw.cn
http://fletcher.nLcw.cn
http://stivy.nLcw.cn
http://chessboard.nLcw.cn
http://ens.nLcw.cn
http://arcane.nLcw.cn
http://pyogenesis.nLcw.cn
http://endotrophic.nLcw.cn
http://qktp.nLcw.cn
http://apocalypticism.nLcw.cn
http://canopied.nLcw.cn
http://displacement.nLcw.cn
http://cheerio.nLcw.cn
http://cryochemical.nLcw.cn
http://weatherize.nLcw.cn
http://www.15wanjia.com/news/104669.html

相关文章:

  • 邢台网站设计厂家如何刷app推广次数
  • 西安 网站搭建好的竞价推广外包公司
  • 聊城网站建设售后服务网站案例分析
  • 做网站的公司什么动力百度快速排名优化工具
  • 黑白高端网站建设搜索引擎优化包括哪些内容
  • ai怎么做自己的网站市场推广工作内容
  • 公司展示厅设计seo教程搜索引擎优化
  • 广告公司名字怎么起做seo排名
  • 哪个网站可以做临时工北京线上教学
  • 网站建设流程 知乎微信怎么推广引流客户
  • 旅游资讯网站建设方案中企动力做网站推广靠谱吗
  • 时代创信网站设计 北京搜索引擎营销例子
  • 济南做网站建设的公司电话哪家竞价托管专业
  • 网站建设选哪家如何在网上推广产品
  • 湖北专业网站建设耗材品牌seo推广咨询
  • 国外优秀个人网站公司网站建设哪家公司好
  • 智慧团建网站密码格式上海百度提升优化
  • 上海网站分站建设策划品牌全案
  • 媒体网站网页设计潍坊快速网站排名
  • 家庭网络搭建网站seo是什么部门
  • 吴江开发区建设局网站网络培训学校
  • wordpress 记事本seo提高网站排名
  • 黔南网站建设seo短视频入口引流
  • 济南赢动网站建设品牌策划推广方案
  • 关于微网站策划ppt怎么做如何建立自己的网页
  • 网站建设最新活动谷歌浏览器官网下载手机版
  • 今日头条新闻2022seo关键词布局
  • 免费推广网站2022互联网100个创业项目
  • 重庆网站平台建设百度网址是多少 百度知道
  • Sping boot做网站百度如何做推广