@Servicepublic class MyService { @Autowired MyComponent myComponent; public String serve() { myComponent.doWork(); return "success"; }}
Quarkus is designed with Substrate VM in mind. For this reason, we encourage you to use *package-private* scope instead of *private*.
package com.bolingcavalry;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import java.time.LocalDateTime;@Path("/actions")public class HobbyResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello RESTEasy, " + LocalDateTime.now(); }}
@Componentpublic class MyComponent { public void doWork() { }}
package com.bolingcavalry.service.impl;import javax.enterprise.context.ApplicationScoped;@ApplicationScopedpublic class ClassAnnotationBean { public String hello() { return "from " + this.getClass().getSimpleName(); }}
package com.bolingcavalry;import com.bolingcavalry.service.impl.ClassAnnotationBean;import javax.inject.Inject;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import java.time.LocalDateTime;@Path("/classannotataionbean")public class ClassAnnotationController { @Inject ClassAnnotationBean classAnnotationBean; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return String.format("Hello RESTEasy, %s, %s", LocalDateTime.now(), classAnnotationBean.hello()); }}
package com.bolingcavalry;import com.bolingcavalry.service.impl.ClassAnnotationBean;import io.quarkus.test.junit.QuarkusTest;import org.junit.jupiter.api.Test;import static io.restassured.RestAssured.given;import static org.hamcrest.CoreMatchers.containsString;@QuarkusTestclass ClassAnnotationControllerTest { @Test public void testGetEndpoint() { given() .when().get("/classannotataionbean") .then() .statusCode(200) // 检查body内容,是否含有ClassAnnotationBean.hello方法返回的字符串 .body(containsString("from " + ClassAnnotationBean.class.getSimpleName())); }}
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 5.702 s[INFO] Finished at: 2022-03-12T15:48:45+08:00[INFO] ------------------------------------------------------------------------
@Component public class Calculator { public int sum(int a, int b) { return a+b; } @Bean public MyBean myBean() { return new MyBean(); } }
package com.bolingcavalry.service;public interface HelloService { String hello();}
package com.bolingcavalry.service;public interface HelloService { String hello();}
package com.bolingcavalry.service.impl;import com.bolingcavalry.service.HelloService;public class HelloServiceImpl implements HelloService { @Override public String hello() { return "from " + this.getClass().getSimpleName(); }}
package com.bolingcavalry.service.impl;import com.bolingcavalry.service.HelloService;import javax.enterprise.context.ApplicationScoped;import javax.enterprise.inject.Produces;public class MethodAnnonationBean { @Produces @ApplicationScoped public HelloService getHelloService() { return new HelloServiceImpl(); }}
package com.bolingcavalry;import com.bolingcavalry.service.HelloService;import javax.inject.Inject;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import java.time.LocalDateTime;@Path("/methodannotataionbean")public class MethodAnnotationController { @Inject HelloService helloService; @GET @Produces(MediaType.TEXT_PLAIN) public String get() { return String.format("Hello RESTEasy, %s, %s", LocalDateTime.now(), helloService.hello()); }}
package com.bolingcavalry;import com.bolingcavalry.service.impl.HelloServiceImpl;import io.quarkus.test.junit.QuarkusTest;import org.junit.jupiter.api.Test;import static io.restassured.RestAssured.given;import static org.hamcrest.CoreMatchers.containsString;@QuarkusTestclass MethodAnnotationControllerTest { @Test public void testGetEndpoint() { given() .when().get("/methodannotataionbean") .then() .statusCode(200) // 检查body内容,HelloServiceImpl.hello方法返回的字符串 .body(containsString("from " + HelloServiceImpl.class.getSimpleName())); }}
public class MethodAnnonationBean { @Produces @ApplicationScoped public HelloService getHelloService(OtherService otherService) { return new HelloServiceImpl(); }}
public class MethodAnnonationBean { @ApplicationScoped public HelloService getHelloService() { return new HelloServiceImpl(); }}
package com.bolingcavalry.service.impl;import com.bolingcavalry.service.HelloService;public class OtherServiceImpl { public String hello() { return "from " + this.getClass().getSimpleName(); }}
package com.bolingcavalry.service.impl;import javax.enterprise.context.ApplicationScoped;import javax.enterprise.inject.Produces;public class FieldAnnonationBean { @Produces @ApplicationScoped OtherServiceImpl otherServiceImpl = new OtherServiceImpl();}
@Path("/fieldannotataionbean")public class FieldAnnotationController { @Inject OtherServiceImpl otherServiceImpl; @GET @Produces(MediaType.TEXT_PLAIN) public String get() { return String.format("Hello RESTEasy, %s, %s", LocalDateTime.now(), otherServiceImpl.hello()); }}
@BuildStep@Record(STATIC_INIT)SyntheticBeanBuildItem syntheticBean(TestRecorder recorder) { return SyntheticBeanBuildItem.configure(Foo.class).scope(Singleton.class) .runtimeValue(recorder.createFoo("parameters are recorder in the bytecode")) .done();}
(责任编辑:探索)
高能电玩节:怪核叙事游戏《吃香》公布概念预告,酷似古早电视广告
聚利宝控股(08527.HK):李朝昌辞任独立非执行董事 3月11日起生效