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

网站最新点击量排名沙田镇仿做网站

网站最新点击量排名,沙田镇仿做网站,最近军事动态,网站页面规范文章目录 使用思路基本使用规范常见问题——Async无效常见问题——添加EnableAsync注解后接口404 使用思路 异步场景及优势在此不多赘述。异步思路无非是在原本请求链路执行到某个环节时#xff0c;将部分无需同步执行的操作交由主线程以外的其它线程执行。因此针对标题中两个… 文章目录 使用思路基本使用规范常见问题——Async无效常见问题——添加EnableAsync注解后接口404 使用思路 异步场景及优势在此不多赘述。异步思路无非是在原本请求链路执行到某个环节时将部分无需同步执行的操作交由主线程以外的其它线程执行。因此针对标题中两个注解的使用我们常常会将异步执行内容单独封装一个方法并通过Async修饰然后在启动类/配置类在异步方法所在类等其它地方也行但规范要求是写在这两处开启EnableAsync SpringBootApplication EnableAsync ComponentScan(value {com.ivan.*}) public class Main {public static void main(String[] args) {SpringApplication.run(Main.class, args);} }public interface DemoService {String testOne() throws InterruptedException;String testTwo() throws InterruptedException;String testThree(); }RestController RequestMapping(/test) public class DemoServiceImpl implements DemoService {AutowiredApplicationContext applicationContext;ResourceLazyprivate DemoServiceImpl demoServiceImpl;OverrideGetMapping(/test_one)public String testOne() throws InterruptedException {//获取HelloService代理对象DemoServiceImpl bean applicationContext.getBean(DemoServiceImpl.class);System.out.println(当前对象是否是代理对象: AopUtils.isAopProxy(bean));System.out.println(是否是cglib代理对象: AopUtils.isCglibProxy(bean));System.out.println(是否是jdk代理对象: AopUtils.isJdkDynamicProxy(bean));System.out.println(bean this);//调用代理对象的异步方法bean.asyncMethod();return LocalDateTime.now().toString();}OverrideGetMapping(/test_two)public String testTwo() throws InterruptedException { // asyncMethod();System.out.println(当前对象是否是代理对象: AopUtils.isAopProxy(demoServiceImpl));System.out.println(是否是cglib代理对象: AopUtils.isCglibProxy(demoServiceImpl));System.out.println(是否是jdk代理对象: AopUtils.isJdkDynamicProxy(demoServiceImpl));demoServiceImpl.asyncMethod();return LocalDateTime.now().toString();}OverrideGetMapping(/test_three)public String testThree() {new Thread(() - {try {Thread.sleep(10000);System.out.println(睡眠结束);} catch (InterruptedException e) {throw new RuntimeException(e);}}).start();return LocalDateTime.now().toString();}Async//不能声明为private//返回值只能为void或者Futurepublic void asyncMethod() throws InterruptedException {Thread.sleep(10000);System.out.println(睡眠结束);} }基本使用规范 Async标注的异步方法返回值只能为void或者Future且方法不能声明为private否则注解会失效。当其它线程调用这个方法时就会开启一个新的子线程去异步处理该业务逻辑。 切记异步方法返回值为Future时在调用方法的同时不要直接.get()否则虽然开启了额外的线程但主方法其实也堵塞在get()这行代码了相当于就还是同步方法了。应当先调用异步方法返回FutureObject在通过Future对象.get()获取结果数据 使用此注解的方法的类对象必须是spring管理下的bean对象 (如被Service、Component等修饰的Bean对象)Async注解还可用于修饰类表示该类中的所有方法都是异步的在Spring项目中需要进行异步编程时一定要开启异步。在启动类/配置类添加EnableAsync 常见问题——Async无效 最常见情况——忘记加EnableAsync注解Async标注的异步方法声明为private没有走Spring的代理类。即Service里面方法A调用方法B,会不生效 方法一定要从另一个类中调用也就是从类的外部调用类的内部调用是无效的需要先获取其代理类通过代理类调用异步方法——异步方法所在类使用了异步注解Async 但是没有实现接口的时候代码底层走 Cglib 动态代理DemoServiceImpl控制类注入到 SpringMVC 容器中。Cglib 动态代理生成的代理对象是采用继承目标对象(DemoServiceImpl)的模式生成代理类的而目标对象(DemoServiceImpl)中有RestController 注解所以注解会被继承过来这样Cglib 生成的代理对象就可以注入到SpringMVC 容器中。因为Async注解方法与控制类在同一个类中导致没有走代理类所以Async 注解失效。 解决方法一新建一个类将Async标注的异步方法放入该类 解决方法二从spring上下文中取得代理对象DemoServiceImpl bean applicationContext.getBean(DemoServiceImpl.class); 解决方法三 在同一个类内部使用self-invocation的方式来调用被Async修饰的方法 Resource Lazy private DemoServiceImpl demoServiceImpl; 常见问题——添加EnableAsync注解后接口404 异步方法所在类实现了接口且使用了异步注解Async代码底层会走 JDK 动态代理导致 OrderServiceImpl 控制类没有注入到 SpringMVC 容器中。因为 JDK 动态代理技术是基于接口实现的而接口中没有RestController注解所以导致代理对象无法注册到SpringMVC容器中。DemoServiceImpl做为代理类实现 DemoService接口代理类(DemoServiceImpl)发现 DemoService接口上面没有 RestController 注解所以导致了代理类(DemoServiceImpl)不会注入到SpringMVC容器中。 Async标注的异步方法所在类实现了接口并重写了方法且EnableAsync注解没有手动注明属性proxyTargetClass true 解决方法EnableAsync(proxyTargetClass true) Spring源码 /** 初始化 bean 对象* Detects handler methods at initialization.* see #initHandlerMethods*/Overridepublic void afterPropertiesSet() {initHandlerMethods();}/*** Scan beans in the ApplicationContext, detect and register handler methods.* see #getCandidateBeanNames() 获取到所有的 bean 对象* see #processCandidateBean* see #handlerMethodsInitialized*/protected void initHandlerMethods() {for (String beanName : getCandidateBeanNames()) {if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {processCandidateBean(beanName);}}handlerMethodsInitialized(getHandlerMethods());}/*** Determine the type of the specified candidate bean and call* {link #detectHandlerMethods} if identified as a handler type.* pThis implementation avoids bean creation through checking* {link org.springframework.beans.factory.BeanFactory#getType}* and calling {link #detectHandlerMethods} with the bean name.* param beanName the name of the candidate bean* since 5.1* see #isHandler* see #detectHandlerMethods*/protected void processCandidateBean(String beanName) {Class? beanType null;try {beanType obtainApplicationContext().getType(beanName);}catch (Throwable ex) {// An unresolvable bean type, probably from a lazy bean - lets ignore it.if (logger.isTraceEnabled()) {logger.trace(Could not resolve type for bean beanName , ex);}}if (beanType ! null isHandler(beanType)) {detectHandlerMethods(beanName);}}/*** {inheritDoc} 判断类上面是否有 Controller、RequestMapping 注解。* pExpects a handler to have either a type-level {link Controller}* annotation or a type-level {link RequestMapping} annotation.*/Overrideprotected boolean isHandler(Class? beanType) {return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));}参考文章 【异步任务】Async注解使用方法及注解失效解决办法 SpringBoot中EnableAsync和Async注解的使用 Async 注解失效解析
http://www.zqtcl.cn/news/764773/

相关文章:

  • 网站策划书包括哪些内容百度官方营销推广平台有哪些
  • 成都企业网站seo重庆企业网站推广费用
  • 广东电白建设集团有限公司网站wordpress 静态地址
  • 微网站和手机站区别工业设计专业学什么
  • 兰州网站建设哪里好素材图片高清
  • 公司网站建设进度设计官网登录入口
  • 中牟高端网站建设wordpress可视化文章
  • 那家公司做网站广西网络营销外包公司
  • 成品网站速成网站知名网站建设加盟合作
  • 零基础学pytho 网站开发Drupal对比WordPress
  • 网站开发 例子快影
  • 宁津建设局网站推介网站
  • c 是用来做网站的吗中国营销策划网
  • 在建设部网站首页莒县网页设计
  • 河北省城乡和住房建设厅网站网店代运营托管
  • 彩票网站建设wordpress判断用户权限
  • 简洁大气企业网站源码h5商城网站建设是什么
  • 河间做网站价格wordpress评论导出
  • 网站关键词布局图网站推广与宣传怎么做
  • 小说类网站程序西安移动网站建设
  • 贵州高端网站建设网站做好了怎么做后台
  • 网站建设与管理 答案国外做免费的视频网站有哪些
  • 网站建设电脑端手机端企业网站建设需求调研表
  • 怎么做游戏网站google国际版
  • 学校网站建设发展规划线上推广的渠道有哪些
  • 公主岭网站建设seo网站推广技术
  • 网站建设一次crm管理
  • 电商网站设计公司优选亿企邦wordpress管理员头像
  • 医院做网站需要多少钱wordpress 模板 设计
  • 建设网站的规则建设公司网站的原则