ToB企服应用市场:ToB评测及商务社交产业平台

标题: quarkus依赖注入之十三:其他重要知识点大串讲(终篇) [打印本页]

作者: 冬雨财经    时间: 2023-8-12 09:31
标题: quarkus依赖注入之十三:其他重要知识点大串讲(终篇)
欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览


简化之一:bean注入

  1.   @Inject
  2.   @ConfigProperty(name = "greeting.message")
  3.   String greetingMsg;
复制代码
  1. @ConfigProperty(name = "greeting.message")
  2. String greetingMsg;
复制代码
简化之二:bean构造方法

  1. @ApplicationScoped
  2. public class MyCoolService {
  3.   private SimpleProcessor processor;
  4.   MyCoolService() { // dummy constructor needed
  5.   }
  6.   @Inject // constructor injection
  7.   MyCoolService(SimpleProcessor processor) {
  8.     this.processor = processor;
  9.   }
  10. }
复制代码
  1. @ApplicationScoped
  2. public class MyCoolService {
  3.   private SimpleProcessor processor;
  4.   MyCoolService(SimpleProcessor processor) {
  5.     this.processor = processor;
  6.   }
  7. }
复制代码
简化之三:bean生产方法

  1. class Producers {
  2.   
  3.   @Produces
  4.   @ApplicationScoped
  5.   MyService produceServ
  6.     ice() {
  7.     return new MyService(coolProperty);
  8.   }
  9. }
复制代码
  1. class Producers {
  2.   @ApplicationScoped
  3.   MyService produceService() {
  4.     return new MyService(coolProperty);
  5.   }
  6. }
复制代码
WithCaching注解:避免不必要的多次实例化

  1. @QuarkusTest
  2. public class WithCachingTest {
  3.     @Inject
  4.     Instance<HelloDependent> instance;
  5.     @Test
  6.     public void test() {
  7.         // 第一次调用Instance#get方法
  8.         HelloDependent helloDependent = instance.get();
  9.         helloDependent.hello();
  10.         // 第二次调用Instance#get方法
  11.         helloDependent = instance.get();
  12.         helloDependent.hello();
  13.     }
  14. }
复制代码
  1. @Dependent
  2. public class HelloDependent {
  3.     public HelloDependent(InjectionPoint injectionPoint) {
  4.         Log.info("injecting from bean "+ injectionPoint.getMember().getDeclaringClass());
  5.     }
  6.     public String hello() {
  7.         return this.getClass().getSimpleName();
  8.     }
  9. }
复制代码


拦截静态方法



All更加直观的注入

  1. public interface SayHello {
  2.     void hello();
  3. }
复制代码
  1. public class InjectAllTest {
  2.     /**
  3.      * 用Instance接收注入,得到所有SayHello类型的bean
  4.      */
  5.     @Inject
  6.     Instance<SayHello> instance;
  7.     @Test
  8.     public void testInstance() {
  9.         // instance中有迭代器,可以用遍历的方式得到所有bean
  10.         for (SayHello sayHello : instance) {
  11.             sayHello.hello();
  12.         }
  13.     }
  14. }
复制代码
  1. @QuarkusTest
  2. public class InjectAllTest {
  3.     /**
  4.      * 用All注解可以将SayHello类型的bean全部注入到list中,
  5.      * 这样更加直观
  6.      */
  7.     @All
  8.     List<SayHello> list;
  9.     @Test
  10.     public void testAll() {
  11.         for (SayHello sayHello : list) {
  12.             sayHello.hello();
  13.         }
  14.     }
  15. }
复制代码
  1. @QuarkusTest
  2. public class InjectAllTest {
  3.    
  4.     @All
  5.     List<InstanceHandle<SayHello>> list;
  6.     @Test
  7.     public void testQuarkusAllAnnonation() {
  8.         for (InstanceHandle<SayHello> instanceHandle : list) {
  9.             // InstanceHandle#get可以得到注入bean
  10.             SayHello sayHello = instanceHandle.get();
  11.             // InjectableBean封装了注入bean的元数据信息
  12.             InjectableBean<SayHello> injectableBean = instanceHandle.getBean();
  13.             // 例如bean的作用域就能从InjectableBean中取得
  14.             Class clazz = injectableBean.getScope();
  15.             // 打印出来验证
  16.             Log.infov("bean [{0}], scope [{1}]", sayHello.getClass().getSimpleName(), clazz.getSimpleName() );
  17.         }
  18.     }
  19. }
复制代码

统一处理异步事件的异常

  1. public class TestEvent {
  2. }
复制代码
  1. @ApplicationScoped
  2. public class TestEventProducer {
  3.     @Inject
  4.     Event<TestEvent> event;
  5.     /**
  6.      * 发送异步事件
  7.      */
  8.     public void asyncProduce() {
  9.         event.fireAsync(new TestEvent());
  10.     }
  11. }
复制代码
  1. @ApplicationScoped
  2. public class TestEventConsumer {
  3.     /**
  4.      * 消费异步事件,这里故意抛出异常
  5.      */
  6.     public void aSyncConsume(@ObservesAsync TestEvent testEvent) throws Exception {
  7.        throw new Exception("exception from aSyncConsume");
  8.     }
  9. }
复制代码
  1. @QuarkusTest
  2. public class EventExceptionHandlerTest {
  3.     @Inject
  4.     TestEventProducer testEventProducer;
  5.     @Test
  6.     public void testAsync() throws InterruptedException {
  7.        testEventProducer.asyncProduce();
  8.     }
  9. }
复制代码

  1. package com.bolingcavalry.service.impl;
  2. import io.quarkus.arc.AsyncObserverExceptionHandler;
  3. import io.quarkus.logging.Log;
  4. import javax.enterprise.context.ApplicationScoped;
  5. import javax.enterprise.inject.spi.EventContext;
  6. import javax.enterprise.inject.spi.ObserverMethod;
  7. @ApplicationScoped
  8. public class NoopAsyncObserverExceptionHandler implements AsyncObserverExceptionHandler {
  9.     @Override
  10.     public void handle(Throwable throwable, ObserverMethod<?> observerMethod, EventContext<?> eventContext) {
  11.         // 异常信息
  12.         Log.info("exception is - " + throwable);
  13.         // 事件信息
  14.         Log.info("observer type is - " + observerMethod.getObservedType().getTypeName());
  15.     }
  16. }
复制代码

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4