@ApplicationScoped
public class ClassAnnotationBean {
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}
@ApplicationScoped
public class ClassAnnotationBean {
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}@Path("/classannotataionbean")
public class ClassAnnotationController {
    @Inject
    ClassAnnotationBean classAnnotationBean;
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get() {
        return String.format("Hello RESTEasy, %s, %s",
                LocalDateTime.now(),
                classAnnotationBean.hello());
    }
}
package com.bolingcavalry.service.impl;
import io.quarkus.logging.Log;
import javax.enterprise.context.RequestScoped;
@RequestScoped
public class RequestScopeBean {
    /**
     * 在构造方法中打印日志,通过日志出现次数对应着实例化次数
     */
    public RequestScopeBean() {
        Log.info("Instance of " + this.getClass().getSimpleName());
    }
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}package com.bolingcavalry;
import com.bolingcavalry.service.impl.RequestScopeBean;
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("/requestscope")
public class RequestScopeController {
    @Inject
    RequestScopeBean requestScopeBean;
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get() {
        return String.format("Hello RESTEasy, %s, %s",
                LocalDateTime.now(),
                requestScopeBean.hello());
    }
}package com.bolingcavalry;
import com.bolingcavalry.service.impl.RequestScopeBean;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;
@QuarkusTest
class RequestScopeControllerTest {
    @RepeatedTest(10)
    public void testGetEndpoint() {
        given()
                .when().get("/requestscope")
                .then()
                .statusCode(200)
                // 检查body内容,是否含有ClassAnnotationBean.hello方法返回的字符串
                .body(containsString("from " + RequestScopeBean.class.getSimpleName()));
    }
}
@Dependent
public class HelloDependent {
    public HelloDependent(InjectionPoint injectionPoint) {
        Log.info("injecting from bean "+ injectionPoint.getMember().getDeclaringClass());
    }
    public String hello() {
        return this.getClass().getSimpleName();
    }
}@ApplicationScoped
public class DependentClientA {
    @Inject
    HelloDependent hello;
    public String doHello() {
        return hello.hello();
    }
}@QuarkusTest
public class DependentTest {
    @Inject
    DependentClientA dependentClientA;
    @Inject
    DependentClientB dependentClientB;
    @Test
    public void testSelectHelloInstanceA() {
        Class clazz = HelloDependent.class;
        Assertions.assertEquals(clazz.getSimpleName(), dependentClientA.doHello());
        Assertions.assertEquals(clazz.getSimpleName(), dependentClientB.doHello());
    }
} 
                文章题目:Quarkus依赖注入之二:Bean的作用域
                
                网页网址:http://www.csdahua.cn/qtweb/news18/36568.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网