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

标题: quarkus依赖注入之一:创建bean [打印本页]

作者: 傲渊山岳    时间: 2023-7-30 12:06
标题: quarkus依赖注入之一:创建bean
欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
关于依赖注入

  1. @Service
  2. public class MyService {
  3.    
  4.     @Autowired
  5.     MyComponent myComponent;
  6.    
  7.     public String serve() {
  8.         myComponent.doWork();
  9.         return "success";
  10.     }
  11. }
复制代码
关于《quarkus依赖注入》系列

本篇概览

graph LRL1(本篇内容) --> L2-1(官方提醒)L1 --> L2-2(CDI)L1 --> L2-3(创建bean)L2-2 --> L3-1(关于CDI)L2-2 --> L3-2(关于bean)L2-3 --> L3-3(注解修饰在类上)L2-3 --> L3-4(注解修饰在方法上)L2-3 --> L3-5(注解修饰在成员变量上)L2-3 --> L3-6(扩展组件中的synthetic bean)
官方提醒

  1. Quarkus is designed with Substrate VM in mind. For this reason, we encourage you to use *package-private* scope instead of *private*.
复制代码
关于CDI

关于CDI的bean


源码下载

名称链接备注项目主页https://github.com/zq2599/blog_demos该项目在GitHub上的主页git仓库地址(https)https://github.com/zq2599/blog_demos.git该项目源码的仓库地址,https协议git仓库地址(ssh)git@github.com:zq2599/blog_demos.git该项目源码的仓库地址,ssh协议
创建demo工程

  1. package com.bolingcavalry;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6. import java.time.LocalDateTime;
  7. @Path("/actions")
  8. public class HobbyResource {
  9.     @GET
  10.     @Produces(MediaType.TEXT_PLAIN)
  11.     public String hello() {
  12.         return "Hello RESTEasy, " + LocalDateTime.now();
  13.     }
  14. }
复制代码
创建bean实例:注解修饰在类上

  1. @Component
  2. public class MyComponent {
  3.     public void doWork() {}
  4. }
复制代码
  1. package com.bolingcavalry.service.impl;
  2. import javax.enterprise.context.ApplicationScoped;
  3. @ApplicationScoped
  4. public class ClassAnnotationBean {
  5.     public String hello() {
  6.         return "from " + this.getClass().getSimpleName();
  7.     }
  8. }
复制代码
  1. package com.bolingcavalry;
  2. import com.bolingcavalry.service.impl.ClassAnnotationBean;
  3. import javax.inject.Inject;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.core.MediaType;
  8. import java.time.LocalDateTime;
  9. @Path("/classannotataionbean")
  10. public class ClassAnnotationController {
  11.     @Inject
  12.     ClassAnnotationBean classAnnotationBean;
  13.     @GET
  14.     @Produces(MediaType.TEXT_PLAIN)
  15.     public String hello() {
  16.         return String.format("Hello RESTEasy, %s, %s",
  17.                 LocalDateTime.now(),
  18.                 classAnnotationBean.hello());
  19.     }
  20. }
复制代码
  1. package com.bolingcavalry;
  2. import com.bolingcavalry.service.impl.ClassAnnotationBean;
  3. import io.quarkus.test.junit.QuarkusTest;
  4. import org.junit.jupiter.api.Test;
  5. import static io.restassured.RestAssured.given;
  6. import static org.hamcrest.CoreMatchers.containsString;
  7. @QuarkusTest
  8. class ClassAnnotationControllerTest {
  9.     @Test
  10.     public void testGetEndpoint() {
  11.         given()
  12.                 .when().get("/classannotataionbean")
  13.                 .then()
  14.                 .statusCode(200)
  15.                 // 检查body内容,是否含有ClassAnnotationBean.hello方法返回的字符串
  16.                 .body(containsString("from " + ClassAnnotationBean.class.getSimpleName()));
  17.     }
  18. }
复制代码
  1. [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  2. [INFO]
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] BUILD SUCCESS
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] Total time:  5.702 s
  7. [INFO] Finished at: 2022-03-12T15:48:45+08:00
  8. [INFO] ------------------------------------------------------------------------
复制代码

创建bean实例:注解修饰在方法上

  1. @Component
  2. public class Calculator {
  3.      public int sum(int a, int b) {
  4.          return a+b;
  5.      }
  6.      @Bean
  7.      public MyBean myBean() {
  8.          return new MyBean();
  9.      }
  10. }
复制代码
  1. package com.bolingcavalry.service;
  2. public interface HelloService {
  3.     String hello();
  4. }
复制代码
  1. package com.bolingcavalry.service.impl;
  2. import com.bolingcavalry.service.HelloService;
  3. public class HelloServiceImpl implements HelloService {
  4.     @Override
  5.     public String hello() {
  6.         return "from " + this.getClass().getSimpleName();
  7.     }
  8. }
复制代码
  1. package com.bolingcavalry.service.impl;
  2. import com.bolingcavalry.service.HelloService;
  3. import javax.enterprise.context.ApplicationScoped;
  4. import javax.enterprise.inject.Produces;
  5. public class MethodAnnonationBean {
  6.     @Produces
  7.     @ApplicationScoped
  8.     public HelloService getHelloService() {
  9.         return new HelloServiceImpl();
  10.     }
  11. }
复制代码
  1. package com.bolingcavalry;
  2. import com.bolingcavalry.service.HelloService;
  3. import javax.inject.Inject;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.core.MediaType;
  8. import java.time.LocalDateTime;
  9. @Path("/methodannotataionbean")
  10. public class MethodAnnotationController {
  11.     @Inject
  12.     HelloService helloService;
  13.     @GET
  14.     @Produces(MediaType.TEXT_PLAIN)
  15.     public String get() {
  16.         return String.format("Hello RESTEasy, %s, %s",
  17.                 LocalDateTime.now(),
  18.                 helloService.hello());
  19.     }
  20. }
复制代码
  1. package com.bolingcavalry;
  2. import com.bolingcavalry.service.impl.HelloServiceImpl;
  3. import io.quarkus.test.junit.QuarkusTest;
  4. import org.junit.jupiter.api.Test;
  5. import static io.restassured.RestAssured.given;
  6. import static org.hamcrest.CoreMatchers.containsString;
  7. @QuarkusTest
  8. class MethodAnnotationControllerTest {
  9.     @Test
  10.     public void testGetEndpoint() {
  11.         given()
  12.                 .when().get("/methodannotataionbean")
  13.                 .then()
  14.                 .statusCode(200)
  15.                 // 检查body内容,HelloServiceImpl.hello方法返回的字符串
  16.                 .body(containsString("from " + HelloServiceImpl.class.getSimpleName()));
  17.     }
  18. }
复制代码

  1. public class MethodAnnonationBean {
  2.     @Produces
  3.     @ApplicationScoped
  4.     public HelloService getHelloService(OtherService otherService) {
  5.         return new HelloServiceImpl();
  6.     }
  7. }
复制代码
  1. public class MethodAnnonationBean {
  2.     @ApplicationScoped
  3.     public HelloService getHelloService() {
  4.         return new HelloServiceImpl();
  5.     }
  6. }
复制代码
创建bean实例:注解修饰在成员变量上

  1. package com.bolingcavalry.service.impl;
  2. import com.bolingcavalry.service.HelloService;
  3. public class OtherServiceImpl {
  4.     public String hello() {
  5.         return "from " + this.getClass().getSimpleName();
  6.     }
  7. }
复制代码
  1. package com.bolingcavalry.service.impl;
  2. import javax.enterprise.context.ApplicationScoped;
  3. import javax.enterprise.inject.Produces;
  4. public class FieldAnnonationBean {
  5.     @Produces
  6.     @ApplicationScoped
  7.     OtherServiceImpl otherServiceImpl = new OtherServiceImpl();
  8. }
复制代码
  1. @Path("/fieldannotataionbean")
  2. public class FieldAnnotationController {
  3.     @Inject
  4.     OtherServiceImpl otherServiceImpl;
  5.     @GET
  6.     @Produces(MediaType.TEXT_PLAIN)
  7.     public String get() {
  8.         return String.format("Hello RESTEasy, %s, %s",
  9.                 LocalDateTime.now(),
  10.                 otherServiceImpl.hello());
  11.     }
  12. }
复制代码
关于synthetic bean

  1. @BuildStep
  2. @Record(STATIC_INIT)
  3. SyntheticBeanBuildItem syntheticBean(TestRecorder recorder) {
  4.    return SyntheticBeanBuildItem.configure(Foo.class).scope(Singleton.class)
  5.                 .runtimeValue(recorder.createFoo("parameters are recorder in the bytecode"))
  6.                 .done();
  7. }
复制代码
欢迎关注博客园:程序员欣宸

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

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




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