标签: cdi

jax-rs 1.1 具有安全过滤器和依赖项注入,如何实现这一点?

例如,我有以下JAX-RS 1.1方法,该方法接收JWT 令牌,检查它,然后处理或拒绝请求,如下所示:

@GET
public Response getBook(@HeaderParam("Authorization") String authorizationToken, @QueryParam("id") String bookId) {

    //if(authorizationToken is a valid JWT token)
    //      get User object which represented by this token
    //      get required book from database
    //      return 200 code with a book within response
    //else if(authorizationToken is invalid by whatever reason it's)
    //      return 401 code within a response
    //else if(database error)
    //      return 500 code within a response

}
Run Code Online (Sandbox Code Playgroud)

正如您在每个 jax-rs 方法中看到的,我需要使用相同的代码行:检查 token将其转换为 …

servlets jax-rs cdi servlet-filters jakarta-ee

4
推荐指数
1
解决办法
1732
查看次数

WELD-001303:Websphere 上范围类型 javax.enterprise.context.SessionScoped 没有活动上下文

我们已升级我们的应用程序以使用 CDI bean。当我们在 Wildfly 10.x 上部署应用程序时,这种更改非常顺利,但是当我们尝试在 Websphere Classic 和 Liberty 上部署相同的应用程序时,出现了一些问题。

我们查找了这里已经发布的几个问题,例如thisthisthisthis,但没有一个答案能够解决我们的问题。

在我的本地主机上,我使用带有 webProfile-7.0 的 Websphere Liberty Profile,即 CDI-1.2、EL-3.0、JSF-2.2 和 servlet-3.1。我们的应用程序还使用 Primefaces 6.0。

问题发生在 PhaseListener 上。在其上,我们注入一个带有@Named(javax.inject.Named) 和@SessionScoped(javax.enterprise.context.SessionScoped) 注释的 Bean。

当在 PhaseListener 上调用注入的变量时,会引发以下错误。

[err] 2017-05-10 09:45:06 ERROR MWExceptionHandler:139 - A server exception occurred
org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:691)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:89)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:164)
at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
at web.frmwrk.mgbean.WebSession$Proxy$_$$_WeldClientProxy.getLocale(Unknown Source)
at web.frmwrk.application.LocaleFaceletViewHandler.calculateLocale(LocaleFaceletViewHandler.java:43) …
Run Code Online (Sandbox Code Playgroud)

websphere primefaces cdi phaselistener jsf-2.2

4
推荐指数
1
解决办法
5358
查看次数

@ApplicationScoped 必须是可序列化的?

我现在正在学习Java EE 7 教程中的 CDI 范围,并发现在最后一段中它说

使用会话、应用程序或对话范围的 Bean 必须是可序列化的,但使用请求范围的 Bean 不必是可序列化的。

但让我很困惑的是,在我的 IDE(Netbeans 和 IntelliJ Idea)中,当我使用 @SessionScoped 或 @ConversationScoped 时,如果我没有像 Java EE 7 教程中所说的那样实现可序列化,它确实会给我一个错误,显然,我无法构建该项目然后运行它。当我使用 @ApplicationScoped 但没有实现 Serialized 时,事情变得很奇怪,没有错误出现,我可以正常构建然后运行应用程序。

所以我对此很好奇,也很想知道为什么。您能解释一下那里发生了什么吗?太感谢了!

java serialization cdi jakarta-ee

4
推荐指数
1
解决办法
2105
查看次数

如何在@Asynchronous方法中正确使用CDI?

我有一个 JSF 2 应用程序(在 JBoss AS 7.1 之上运行),当用户单击页面中的按钮时,该应用程序必须启动一个很长的进程。其想法是进行非阻塞交互,因此用户可以等待并查看结果,或者简单地关闭页面并稍后返回以查看进展情况或结果(如果该过程已经结束)。

该过程本身在以下(简化的)类中进行编码:

@Stateless
@LocalBean
@ApplicationScoped
public class MyProcessManager {
    @Inject
    private ProcessHelper processHelper;

    @Asynchronous
    public void start(final ProcessParameters parameters) {
        // the process...
    }
}
Run Code Online (Sandbox Code Playgroud)

这样的类被标记为 ,@ApplicationScoped因为所有正在运行的进程(对于所有用户)都由它保存。因此,当单击按钮时,支持 bean 会设置一些参数并调用异步方法start()

一切都很顺利,直到进程尝试使用 时processHelper,它会运行许多 Hibernate 查询以继续进程的持久性部分。processHelper当调用第一个方法时,我得到以下异常:

WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
Run Code Online (Sandbox Code Playgroud)

作为附加信息,此类方法内部的断点永远不会被命中。

发生了什么以及如何解决?

jsf asynchronous ejb cdi

4
推荐指数
1
解决办法
5835
查看次数

在使用 Quarkus 进行本地开发期间注入不同的 Bean

使用 Spring 和 Micronaut,有非常简洁的方法可以根据应用程序运行的环境/配置文件来注入不同的 bean。我正在尝试对 Quarkus 做同样的事情。

我读过这篇文章: https: //quarkus.io/blog/quarkus-dependency-injection/。这个 StackOverflow 帖子中提到了这个过程:How can I override a CDI bean in Quarkus for test? 。最后一篇文章说,“在测试目录中创建 bean”。

我的问题略有不同。我想在“开发”时注入一个bean。在生产中,我想注入默认的 bean。从文档中,我看不出有什么方法可以让应用程序做出这种区分。

如果我有一个像这样的默认类:

@DefaultBean
@ApplicationScoped
class ProdProvider : SomeProvider {}
Run Code Online (Sandbox Code Playgroud)

我想像这样覆盖它:

@Alternative
@Priority(1)
class DevProvider : SomeProvider {}
Run Code Online (Sandbox Code Playgroud)

我怎样才能只在开发模式下发生这种情况?

在一种情况下,我有一个凭据提供程序类,用于在本地开发时设置 Google 的 PubSub 模拟器。在生产中,我使用一个实现相同接口的类,但它是一个真正的凭据提供程序。导致我问这个问题的特殊情况是一个实现一种方法的类:

@ApplicationScoped
class VaultLoginJwtProvider : LoginJwtProvider {
  @ConfigProperty(name = "vault.tokenPath")
  private val jwtPath: String? = null

  companion object {
    val logger: Logger = LoggerFactory.getLogger("VaultTokenProvider")
  }

  override fun getLoginJwt(): Optional<String> {
    logger.info("Using …
Run Code Online (Sandbox Code Playgroud)

cdi quarkus

4
推荐指数
1
解决办法
1万
查看次数

当在字段上指定 时,javax.enterprise.context.RequestScoped 如何工作?

在代码中发现以下内容(真实姓名替换为虚拟姓名):

JAX-RS 资源

@Path("hello")
public class HelloResource {

  @Inject
  @RequestScoped
  FirstService service1;

  @Inject
  SecondService service2;

  ....

}
Run Code Online (Sandbox Code Playgroud)

依赖关系

// first
public class FirstService {

  private static final Logger LOGGER = ...

  @Inject
  HttpServletRequest request;

  ....
}

// second
@ApplicationScoped
public class SecondService { .... } 
Run Code Online (Sandbox Code Playgroud)

允许@RequestScoped在字段上声明。但无法在任何地方找到它是如何工作的。

问题1:如果我指定@RequestScoped将由容器注入的字段,我会在那里获得请求范围的真实注入实例吗?

问题 2:如果我将 DI 更改为基于构造函数会怎样?在这种情况下我应该把它放在哪里@RequestScoped

@Path("hello")
public class HelloResource {

  private final FirstService service1;
  private final SecondService service2;

  @Inject
  public HelloResource(FirstService service1, SecondService service2) {
    // …
Run Code Online (Sandbox Code Playgroud)

java scope cdi java-ee-8

4
推荐指数
1
解决办法
5938
查看次数

在 Quarkus 中使用 @Inject 和接口进行依赖注入

我正在尝试使用 Quarkus 1.6.1.Final 和 OpenJDK 11 解决 Repository Pattern 的依赖注入。我想实现 Inject with Interface 并给它们一些参数(如@Named@Qualifier)来指定具体类,但目前我有UnsatisfiedResolutionException并且不确定如何修复它。

这是我的代码部分。

用例类:

@ApplicationScoped
public class ProductStockCheckUseCase {
    @Inject
    @Named("dummy")
    ProductStockRepository repo;

    public int checkProductStock() {
        ProductStock stock = repo.findBy("");
        return stock.getCount();
    }
}
Run Code Online (Sandbox Code Playgroud)

存储库接口:

public interface ProductStockRepository {
    public ProductStock findBy(String productId);
}
Run Code Online (Sandbox Code Playgroud)

存储库实现:

@Named("dummy")
public class ProductStockDummyRepository implements ProductStockRepository {

    public ProductStock findBy(final String productId) {
        final ProductStock productStock = new ProductStock();
        return productStock;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 build.gradle 依赖项的一部分: …

java spring cdi quarkus

4
推荐指数
1
解决办法
3482
查看次数

错误找不到类型 javax.enterprise.context.ApplicationScoped。您是否缺少对类路径的依赖?

我正在使用 Quarkus 编写后端。下面是我的 POM.xml 文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.abcd.mpt</groupId>
    <artifactId>hc-web</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <properties>
        <compiler-plugin.version>3.11.0</compiler-plugin.version>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>3.1.1.Final</quarkus.platform.version>
        <skipITs>true</skipITs>
        <surefire-plugin.version>3.0.0</surefire-plugin.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-jdbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-hibernate-orm-panache</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-agroal</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-flyway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version> <!-- Please check …
Run Code Online (Sandbox Code Playgroud)

java cdi quarkus jakarta-migration

4
推荐指数
1
解决办法
2528
查看次数

CDI将EJB注入到Glassfish v3上的POJO中

是否可以使用Glassfish v3上的CDI将EJB 3.1 bean注入POJO?

我的类(在EJB模块中):

@Singleton
@LocalBean
@Startup
@Named
public class NewSingletonBean {

    @PostConstruct
    public void init(){
        System.out.println("NewSingletonBean INIT");
    }

}
Run Code Online (Sandbox Code Playgroud)

_

@Singleton
@LocalBean
@Startup
@DependsOn(value="NewSingletonBean")
public class NewSingletonBean2 {

    @Inject NewSingletonBean newSingletonBean;

    @PostConstruct
    public void init(){
        System.out.println("NewSingletonBean2 INIT");
        System.out.println("EJB injected into EJB: " + (newSingletonBean != null));
        MyPOJO p = new MyPOJO();
        p.sth();
    }

}
Run Code Online (Sandbox Code Playgroud)

_

public class MyPOJO {
        @Inject NewSingletonBean newSingletonBean;

        public void sth(){
            System.out.println("EJB injected into POJO: " + (newSingletonBean != null));
        }
}
Run Code Online (Sandbox Code Playgroud)

服务器输出:

Launching …
Run Code Online (Sandbox Code Playgroud)

java ejb cdi ejb-3.1 glassfish-3

3
推荐指数
1
解决办法
4887
查看次数

Netbeans警告事件注入

我在我的LanguageOfSystem bean中使用CDI Observer模式.

@Named(value = "languageOfSystem")
@SessionScoped
public class LanguageOfSystem implements Serializable {

   @Inject private JsfUtils eeJsfUtils;
   @Inject private Event<LangEvent> langEvent;
   private LangEvent docLangEvent = new LangEvent();
Run Code Online (Sandbox Code Playgroud)

LangEvent的简单地说:

package jav;

import java.util.Locale;

public class LangEvent {
   private Locale locale;

   public Locale getLocale() {
      return locale;
   }

   public void setLocale(Locale locale) {
      this.locale = locale;
   }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但NetBeans给了我一个警告: 在此输入图像描述

Unstatisfied dependency: no bean matches the injection point.
Run Code Online (Sandbox Code Playgroud)

我应该检查或更改一些东西吗?

warnings netbeans cdi observer-pattern

3
推荐指数
1
解决办法
3434
查看次数