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

关于网站开发的文档seo短视频网页入口引流下载

关于网站开发的文档,seo短视频网页入口引流下载,如何自建网站视频,电商公司简介模板目录 一、为何需要AOP#xff1f;1、从实际需求出发2、现有的技术能解决吗#xff1f;3、AOP可以解决 二、如何实现AOP#xff1f;1、基本使用2、更推荐的做法2.1 “基本使用”存在的隐患2.2 最佳实践2.2.1 参考Transactional#xff08;通过AOP实现事务管理#xff09;2.… 目录 一、为何需要AOP1、从实际需求出发2、现有的技术能解决吗3、AOP可以解决 二、如何实现AOP1、基本使用2、更推荐的做法2.1 “基本使用”存在的隐患2.2 最佳实践2.2.1 参考Transactional通过AOP实现事务管理2.2.2 自定义注解 3、后言 一、为何需要AOP 1、从实际需求出发 public class Book {...... }public interface IBookService {int insertBook(Book book);int deleteBookById(Long id);int updateBook(Book book);Book selectBookById(Long id); }public class BookServiceImpl implements IBookService {Overridepublic int insertBook(Book book) {// 入参检查// 日志记录// 事务处理// 业务逻辑return 0;}Overridepublic int deleteBookById(Long id) {// 入参检查// 日志记录// 事务处理// 业务逻辑return 0;}Overridepublic int updateBook(Book book) {// 入参检查// 日志记录// 事务处理// 业务逻辑return 0;}Overridepublic Book selectBookById(Long id) {// 入参检查// 日志记录// 事务处理// 业务逻辑return null;} }上述代码的问题 1业务逻辑代码和非业务逻辑代码耦合2非业务逻辑的代码重复度高却没有复用 2、现有的技术能解决吗 方案1模板模式 public abstract class BookServiceTemplate {public T, E T execute(E e) {// 入参检查// 日志记录// 事务处理return doProcess(e);}protected abstract T, E T doProcess(E e); }public class BookServiceImpl implements IBookService {Overridepublic int insertBook(Book book) {return new BookServiceTemplate() {Overrideprotected T, E T doProcess(E e) {return null;}}.doProcess(book);}... }在需求初期BookServiceImpl的4个方法的入参检查、日志记录、事务处理都比较一致时上面的写法还行得通。可一旦其中一个方法如insertBook需要改变入参检查、日志记录、事务处理时事情就变得麻烦了。改模板吧影响了其他方法如updateBook。去掉insertBook方法的模板吧随着需求的发展模板模式彻底腐化或被抛弃了。 方案2代理模式 public class BookServiceProxyImpl implements IBookService {private final IBookService bookService;public BookServiceProxyImpl(IBookService bookService) {this.bookService bookService;}Overridepublic int insertBook(Book book) {// 入参检查boolean pass checkParams(book);// 日志记录// 事务处理// 业务逻辑return bookService.insertBook(book);}...// 入参检查private boolean checkParams(Book book) {...} }挺不错的其中一个方法如insertBook需要改变入参检查、日志记录、事务处理时改变这个方法即可对其他方法没影响。但这个代理类不太“干净”又能看到业务代码逻辑的入口如insertBook又能看到非业务逻辑代码如checkParams。 3、AOP可以解决 将业务逻辑代码和非业务逻辑代码解耦 业务逻辑代码在对象A非业务逻辑代码在对象B Spring管理了对象A和对象B在逻辑执行中交织对象A的业务逻辑和对象B的非业务逻辑这是我对AOP的简单理解。AOPAspect Oriented Programming即面向切面编程 面向对象编程OOP通过将系统的大功能点拆分为一个一个小功能点分别交给不同的类/对象负责封装并利用类的继承、多态构建系统的结构让类相互配合从而让系统运作起来。AOP本质还是OOP是对OOP的补充。 二、如何实现AOP 1、基本使用 示例【来源】 public class User { }public interface IUserService {int insertUser(User user);int deleteUserById(Long id); }Service public class UserServiceImpl implements IUserService {Overridepublic int insertUser(User user) {return 0;}Overridepublic int deleteUserById(Long id) {return 0;} }public class Mail { }public interface IMailService {int insert(Mail mail); }Service public class MailServiceImpl implements IMailService {Overridepublic int insert(Mail mail) {return 0;} }Aspect Component public class LoggingAspect {/*** com.forrest.learnspring.aop.example2.user.service.impl.UserServiceImpl类的所有public xxx 方法(yyy)在执行前都会被拦截br* 并执行这个方法*/Before(execution(public * com.forrest.learnspring.aop.example2.user.service.impl.UserServiceImpl.*(..)))public void doAccessCheck() {System.out.println([Before] do access check...);}/*** com.forrest.learnspring.aop.example2.mail.service.impl.MailServiceImpl类的所有public xxx 方法(yyy)在执行前都会被拦截br* 并执行这个方法*/Around(execution(public * com.forrest.learnspring.aop.example2.mail.service.impl.MailServiceImpl.*(..)))public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {System.out.println([Around] start pjp.getSignature());Object retVal pjp.proceed();System.out.println([Around] end pjp.getSignature());return retVal;} }如果不加EnableAspectJAutoProxy ComponentScan(com.forrest.learnspring.aop.example2) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);String[] beanDefinitionNames applicationContext.getBeanDefinitionNames();Arrays.stream(beanDefinitionNames).forEach(System.out::println);} }/** org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory application loggingAspect mailServiceImpl userServiceImpl */LoggingAspect因为被打上了Component注解因此也被加载成bean了。但实际上loggingAspect没有起任何作用什么都没输出 ComponentScan(com.forrest.learnspring.aop.example2) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);IUserService userService applicationContext.getBean(UserServiceImpl.class);userService.insertUser(new User());} }加上EnableAspectJAutoProxy后 EnableAspectJAutoProxy ComponentScan(com.forrest.learnspring.aop.example2) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);IUserService userService applicationContext.getBean(UserServiceImpl.class);userService.insertUser(new User());} }多了一个beanorg.springframework.aop.config.internalAutoProxyCreator但报错 Exception in thread main org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com.forrest.learnspring.aop.example2.user.service.impl.UserServiceImpl available这就诡异了明明在Spring容器中看到了userServiceImplSpring咋说没有呢很可能是因为使用代理对象作为容器中的实际Bean EnableAspectJAutoProxy ComponentScan(com.forrest.learnspring.aop.example2) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);Object bean applicationContext.getBean(userServiceImpl);System.out.println(bean instanceof Advised); // 带上EnableAspectJAutoProxy注解则为true否则为fasle。} }这么改就对了 EnableAspectJAutoProxy ComponentScan(com.forrest.learnspring.aop.example2) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);IUserService userService applicationContext.getBean(IUserService.class);userService.insertUser(new User());} }或者 EnableAspectJAutoProxy ComponentScan(com.forrest.learnspring.aop.example2) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);IUserService userService (IUserService) applicationContext.getBean(userServiceImpl);userService.insertUser(new User());} }输出 [Before] do access check...2、更推荐的做法 2.1 “基本使用”存在的隐患 目标代码如userService.insertUser(new User());无法感知到自己会被拦截。 2.2 最佳实践 2.2.1 参考Transactional通过AOP实现事务管理 对于一个bean我希望这个bean的insertUser方法开启事务可以这么写 Service public class UserServiceImpl implements IUserService {OverrideTransactionalpublic int insertUser(User user) {return 0;}...... }// 需要依赖: dependencygroupIdorg.springframework/groupIdartifactIdspring-tx/artifactIdversion5.2.25.RELEASE/version /dependency通过注解可以“自主”告知Spring代理“我”吧我需要AOP。 2.2.2 自定义注解 跟着廖雪峰老师实现对计算方法执行的耗时。 public class User { }public interface IUserService {User register(String email, String password, String name); }Service public class UserServiceImpl implements IUserService {OverrideMetricTime(register)public User register(String email, String password, String name) {try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}return new User();} }Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface MetricTime {String value(); }Aspect Component public class MetricAspect {Around(annotation(metricTime))public Object metric(ProceedingJoinPoint pjp, MetricTime metricTime) throws Throwable {long start System.currentTimeMillis();try {return pjp.proceed();} finally {long end System.currentTimeMillis();System.out.println([Metric] [ metricTime.value() ] time cost: (end - start));}} }annotation(xxx)中的xxx和MetricTime xxx要一一对应。 EnableAspectJAutoProxy ComponentScan(com.forrest.learnspring.aop.example4.user) public class Application {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Application.class);IUserService userService applicationContext.getBean(IUserService.class);userService.register(forrestgmail.com, 123456, forrest);} }/** [Metric] [register] time cost: 1001 */3、后言 实际开发中写AOP代码比较少主要是业务代码本身难以完全拨离非业务代码。 1例如打日志。不可能只在方法的前后打日志在方法执行中也需要加一些日志。2例如参数检查。在方法执行前做参数检查只是一些基本的检查。另外有些参数的校验本身就属于业务逻辑的一部分。抛开业务本身是没法判断参数是否正确的。3例如事务处理。可以用Spring提供的Transactional注解。即使要自己定义注解通过AOP实现事务在写方法体时也要格外注意别带一些没必要参与事务的逻辑。 不过真需要在执行某些方法时做一些拦截处理AOP还是不错的选择。
http://www.dnsts.com.cn/news/150597.html

相关文章:

  • 网站报备之后如何建设网站做网站购买虚拟主机送模板吗
  • 网站模板类型网站设计的公司怎么样
  • 天津黑曼巴网站建设网页设计尺寸分辨率
  • 甜品网站模板代码手机网页游戏开发
  • 营销神器湖南网站优化
  • 有了虚拟主机怎么做网站cosy主题wordpress
  • 最便宜的网站空间电子商务岗位有哪些
  • 上海企业网站制作哪家好python做网站的好处
  • 网站风格类型有哪些互联网推广图片
  • 织梦网站栏目添加产品网络推广方案设计
  • 花样云做网站怎样济源建设网站
  • 网页设计与制作的实训报告seo优化费用
  • 局网站建设申请网站支付体现功能怎么做
  • 免费空间网站源码网站前台 后台
  • 做设计的素材网站有哪些网站开发主要技术
  • 天津企业网站专业订制网站开发需要用哪些东西
  • 网站编写教程青岛抖音广告
  • 做落地页素材在什么网站上找wordpress标签工具栏
  • 耒阳市做网站的如何做电影网站赚钱
  • 连云港做网站设计西安市住房和城乡建设局
  • 天津个人网站建设怎么让网站被收录
  • 国外交互设计网站欣赏网站建设公司山而
  • 湖南营销型网站建设 干净磐石网络网页制作工具按其制作方式分可以分为哪几种
  • 如何做国外网站推广小程序设计流程
  • 英文网站怎么做推广门户网站维护方案
  • wordpress 设置404页面孝感网站seo
  • 宁波seo公司网站推广夫唯seo视频教程
  • wordpress菜单显示选项打不开关键词seo排名怎么样
  • 我的网站域名马铃薯交易网站建设方案
  • asp手机网站源码下载网页制作需要学多久