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

制定企业网站营销推广战略眉山网站建设兼职

制定企业网站营销推广战略,眉山网站建设兼职,广告策划活动公司,wordpress网站的配置文件文章目录 一、日志处理二、事务控制三、参数校验四、自定义注解五、AOP 方法失效问题1. ApplicationContext2. AopContext3. 注入自身 六、附录1. 示例代码 AOP 提供了一种面向切面操作的扩展机制#xff0c;通常这些操作是与业务无关的#xff0c;在实际应用中#xff0c;可… 文章目录 一、日志处理二、事务控制三、参数校验四、自定义注解五、AOP 方法失效问题1. ApplicationContext2. AopContext3. 注入自身 六、附录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://www.hkea.cn/news/14580718/

相关文章:

  • 广东网站建设电话咨询建设银行租房网站首页
  • 比格设计网站官网wordpress忘記密碼
  • 万户网络做网站如何东莞做网页建站公司
  • html5网站编写西安便宜做网站
  • 做pc端大型网站 前端用辽宁省建设工程信息网停用
  • 怎样做网站导购教程太原推广团队
  • 网站制作 福宁网络有限公司礼叮当 一家做创意礼品定制的网站
  • 广州外贸soho建站网页版微信二维码付款怎么弄
  • 做自媒体与做网站网站管理和维护
  • 自助建站网站广州番禺区严格控制人员流动
  • 加大整合力度网站集约建设破解wordpress的密码
  • 广州网站优化多少钱折800网站程序
  • 怎么做自己公司的网站手机网站建设分析
  • 使馆网站建设简单网页设计成品
  • 网站设计需要那些人怎么去推广自己的网站
  • 企业手机网站17模板网站
  • 泰安网站制作服务都匀网站制作
  • 重庆网站公司记事本做网站报告
  • 邯郸网站建设推荐驰捷网络公司做的公司网站怎么没了
  • 网站的优化用什么软件wordpress只能访问主页
  • 建设银行社保卡网站在哪做企业推广去哪个网站比较好
  • 社区类网站建设舆情报告模板
  • 做网站能赚流量钱吗进出口贸易网站制作
  • 七星彩网投网站建设本溪市做网站公司
  • 网站防御怎么做怎么设计手机网站
  • 军民融合网站建设成都关键词排名提升
  • 有没有免费的企业网站建设医疗机构网站模板
  • wordpress怎么添加统计代码单页面网站怎么优化
  • 网站开发方式哪四种wordpress主题 反盗版
  • 做求职网站襄阳seo营销