注入的豆在我的quarkus扩展中无效

发布于 2025-01-23 19:35:30 字数 1396 浏览 0 评论 0原文

我有一个非常简单的Quarkus扩展名,它定义了一个容器RequestFilter来过滤身份验证并将数据添加到自定义AuthenticationContext。

这是我的代码:

runtime/AuthenticationContext.java

public interface AuthenticationContext {
  User getCurrentUser();
}

Runtime/AuthenticationContextimpl.java

@RequestScoped
public class AuthenticationContextImpl implements AuthenticationContext {
    private User user;

    @Override
    public User getCurrentUser() {
        return user;
    }

    public void setCurrentUser(User user) {
        this.user = user;
    }
}

runtime/myFilter.java

@ApplicationScoped
public class MyFilter implements ContainerRequestFilter  {

  @Inject
  AuthenticationContextImpl authCtx;

  @Override
  public void filter(ContainerRequestContext requestContext){

    // doing some stuff like retrieving the user from the request Context
    // ...

    authCtx.setCurrentUser(retrievedUser)
    
  }
}

deployment/myProcessor.java < /code>:

class MyProcessor {

  //... Some stuff

  @BuildStep
  AdditionalBeanBuildItem createContext() {
      return new AdditionalBeanBuildItem(AuthenticationContextImpl.class);
  }
}

我在authctx.setcurrentuser(retereduser)呼叫(永远不会注入authctx)中有一个无效的指针异常

谢谢

I have a quite simple quarkus extension which defines a ContainerRequestFilter to filter authentication and add data to a custom AuthenticationContext.

Here is my code:

runtime/AuthenticationContext.java

public interface AuthenticationContext {
  User getCurrentUser();
}

runtime/AuthenticationContextImpl.java

@RequestScoped
public class AuthenticationContextImpl implements AuthenticationContext {
    private User user;

    @Override
    public User getCurrentUser() {
        return user;
    }

    public void setCurrentUser(User user) {
        this.user = user;
    }
}

runtime/MyFilter.java

@ApplicationScoped
public class MyFilter implements ContainerRequestFilter  {

  @Inject
  AuthenticationContextImpl authCtx;

  @Override
  public void filter(ContainerRequestContext requestContext){

    // doing some stuff like retrieving the user from the request Context
    // ...

    authCtx.setCurrentUser(retrievedUser)
    
  }
}

deployment/MyProcessor.java:

class MyProcessor {

  //... Some stuff

  @BuildStep
  AdditionalBeanBuildItem createContext() {
      return new AdditionalBeanBuildItem(AuthenticationContextImpl.class);
  }
}

I have a Null Pointer Exception in authCtx.setCurrentUser(retrievedUser) call (authCtx is never injected)

What am I missing here ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2025-01-30 19:35:30

索引扩展名的Runtime模块解决了问题。

Indexing the runtime module of the extension fixes the problem.
There are multiple ways to do that as mentioned in https://stackoverflow.com/a/55513723/2504224

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文