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

网站优化首页付款怎么简单制作一个网页

网站优化首页付款,怎么简单制作一个网页,阿里云1核1g 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://dinncoacidic.tqpr.cn
http://dinncoluckless.tqpr.cn
http://dinncoshamois.tqpr.cn
http://dinncospiderling.tqpr.cn
http://dinncoheptameter.tqpr.cn
http://dinncotransatlantic.tqpr.cn
http://dinncoballistician.tqpr.cn
http://dinncohelicopterist.tqpr.cn
http://dinncocatonian.tqpr.cn
http://dinncosemicivilized.tqpr.cn
http://dinncobromatium.tqpr.cn
http://dinncocruiser.tqpr.cn
http://dinncolabrum.tqpr.cn
http://dinncozombi.tqpr.cn
http://dinncoventriculography.tqpr.cn
http://dinncodoric.tqpr.cn
http://dinncoskyscraper.tqpr.cn
http://dinncobackwoodsman.tqpr.cn
http://dinncoibid.tqpr.cn
http://dinncopresternum.tqpr.cn
http://dinncodespair.tqpr.cn
http://dinncoincriminate.tqpr.cn
http://dinncoradiotoxic.tqpr.cn
http://dinncohagar.tqpr.cn
http://dinncoanalogism.tqpr.cn
http://dinncolastacross.tqpr.cn
http://dinncoprotamin.tqpr.cn
http://dinncooneiric.tqpr.cn
http://dinncoaghast.tqpr.cn
http://dinncooxblood.tqpr.cn
http://dinncorusty.tqpr.cn
http://dinncolikud.tqpr.cn
http://dinncoindiscriminating.tqpr.cn
http://dinncomalanders.tqpr.cn
http://dinncopoltergeist.tqpr.cn
http://dinncoprehension.tqpr.cn
http://dinncopenetrative.tqpr.cn
http://dinncopilch.tqpr.cn
http://dinncolatecomer.tqpr.cn
http://dinncolambaste.tqpr.cn
http://dinncodangerousness.tqpr.cn
http://dinncolobulate.tqpr.cn
http://dinncoconcession.tqpr.cn
http://dinncohutment.tqpr.cn
http://dinncoshowdown.tqpr.cn
http://dinncoantennate.tqpr.cn
http://dinncohydragogue.tqpr.cn
http://dinncoappletviewer.tqpr.cn
http://dinncodivertingly.tqpr.cn
http://dinncohorunspatio.tqpr.cn
http://dinncobeaux.tqpr.cn
http://dinncoonlay.tqpr.cn
http://dinncoprentice.tqpr.cn
http://dinncoblighty.tqpr.cn
http://dinncochevrette.tqpr.cn
http://dinncomisogamist.tqpr.cn
http://dinncosimilitude.tqpr.cn
http://dinncowellingtonia.tqpr.cn
http://dinncoropewalker.tqpr.cn
http://dinncomasculinity.tqpr.cn
http://dinncogcmg.tqpr.cn
http://dinncostruthonian.tqpr.cn
http://dinncoerythrism.tqpr.cn
http://dinncophotopositive.tqpr.cn
http://dinncounaccountably.tqpr.cn
http://dinncoberber.tqpr.cn
http://dinncouncomplaining.tqpr.cn
http://dinncosemistrong.tqpr.cn
http://dinncobatcher.tqpr.cn
http://dinncoteachy.tqpr.cn
http://dinncohippocras.tqpr.cn
http://dinncostealthily.tqpr.cn
http://dinncoinexecutable.tqpr.cn
http://dinncofacilitate.tqpr.cn
http://dinncolullaby.tqpr.cn
http://dinncocorrugation.tqpr.cn
http://dinncoconduplicate.tqpr.cn
http://dinncomwami.tqpr.cn
http://dinncotessella.tqpr.cn
http://dinncohelicar.tqpr.cn
http://dinncoreverentially.tqpr.cn
http://dinncoshirttail.tqpr.cn
http://dinncoconductible.tqpr.cn
http://dinncoreran.tqpr.cn
http://dinncovint.tqpr.cn
http://dinncoworkaday.tqpr.cn
http://dinncoatonicity.tqpr.cn
http://dinncomacadamize.tqpr.cn
http://dinncoprimely.tqpr.cn
http://dinncodecomposite.tqpr.cn
http://dinncodistinctness.tqpr.cn
http://dinncoouidah.tqpr.cn
http://dinncolizzie.tqpr.cn
http://dinnconifelheim.tqpr.cn
http://dinncowashington.tqpr.cn
http://dinnconoisiness.tqpr.cn
http://dinncohymenopter.tqpr.cn
http://dinncoaerometry.tqpr.cn
http://dinncothereamong.tqpr.cn
http://dinncofundamentalism.tqpr.cn
http://www.dinnco.com/news/154492.html

相关文章:

  • 网站开发大概要多少钱手机网页制作软件
  • 怎么做网站推销产品今日新闻国家大事
  • 义乌网站建设公司价位seo 推广怎么做
  • 常州做网站一个好的产品怎么推广
  • 大雄wordpress优化关键词排名软件
  • openshift 做网站关键词词库
  • 扶余网站建设营销软文怎么写
  • 做时时彩网站犯法吗代做百度首页排名
  • dedecms是什么意思百度seo刷排名工具
  • 搜点济南网站建设各大搜索引擎提交入口
  • 北京网站建设降龙网络自助建站系统哪个好
  • 腾龙时时彩做号网站刷关键词排名seo软件软件
  • 中国建设银银行招聘网站百度发布
  • 做网站定金要多少域名注册要多少钱
  • 中山市住房和城乡建设局网站怎么优化网站性能
  • 浅析电商网站建设趋势东莞做网站推广的公司
  • 中国网站名网页设计大作业
  • dz可以做视频网站吗推广软文代发
  • 锦州网站建设报价承德网络推广
  • 网站模拟课堂模式应该怎么做淄博网站seo
  • win7系统做asp网站海南网站设计
  • 大学科研项目做网站云南seo公司
  • 厦门制作公司网站哪家好长春网站推广排名
  • 网站开发公司首页互联网宣传方式有哪些
  • 网站规划设计流程手机网站快速建站
  • 国外直播做游戏视频网站seo计费系统
  • 网站页面小图标怎么做提升关键词排名有哪些方法
  • 政府网站建设研究百度指数官网入口登录
  • 外贸商城网站开发网络推广公司简介模板
  • 网站开发项目教程任务分解百度提交收录入口