在RequestScoped bean CDI上的HttpServletRequest注入 [英] HttpServletRequest injection on RequestScoped bean CDI

查看:135
本文介绍了在RequestScoped bean CDI上的HttpServletRequest注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将@RequestScoped定制类注入到我的@Stateless JAX-RS端点中的方法:

I'm looking for a way in order to inject a @RequestScoped custom class into my @Stateless JAX-RS endpoint:

我希望每次应用程序收到请求时,都会将我的自定义类注入到我的JAX-RS端点中.

I want each time the application receives a request my custom class is injected in my JAX-RS endpoint.

自定义类:

@RequestScoped
public class CurrentTransaction {

    private String user;
    private String token;

    @PersistenceContext(name="mysql")
    protected EntityManager em;

    @Inject HttpServletRequest request;

    public CurrentTransaction() {
        this.user = request.getHeader("user");
        this.token = request.getHeader("token");
    }

    //getters and setters ...
}

因此,我将我的CurrentTransaction类声明为@RequestScoped,以便在每次接收到请求时进行初始化. 为此,我需要访问HttpServletResquest以获得标题参数.

So, I declare my CurrentTransaction class as @RequestScoped in order to be initialized each time a request is received. In order to do this, I need to access to HttpServletResquest in order to get header parameters.

JAX-RS端点:

@Stateless
@Path("/areas")
public class AreasEndpoint {

   @PersistenceContext(unitName = "mysql")
   protected EntityManager em;

   @Inject
   protected CurrentTransaction current_user_service;

    @POST
    @Path("postman")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Authentication
    public Response create(AreaRequest request) {

        if (this.current_user_service.getUser() == null) {
            System.out.println("Go!!!");
            return Response.status(Status.FORBIDDEN).build();
        } else {
            System.out.println("---- user: " + this.current_user_service.getUser());
            System.out.println("---- token: " + this.current_user_service.getToken());
        }
    }

    // ...
}

CDI到达以执行CurrentTransaction类的构造函数.但是,HttpServletRequest请求字段未初始化(注入).

CDI arrive to perform the constructor of CurrentTransaction class. However, HttpServletRequest request field is not initialized (injected).

我在做什么错了?

推荐答案

对此的最新解答-可能对其他读者有用:

A late answer on this one--maybe useful to other readers: dependency injection in CDI is done in the following order:

  1. 构造函数被调用
  2. 注入了田地
  3. 调用
  4. @PostConstruct 注释的方法

最后一点是您要介入以进行进一步的初始化,需要对注入的字段进行访问:

The last point is where you want to step in for further initialization that needs access on the injected fields:

@Inject HttpServletRequest request;

public CurrentTransaction() {
    // field injection has not yet taken place here
}

@PostConstruct
public void init() {
    // the injected request is now available
    this.user = request.getHeader("user");
    this.token = request.getHeader("token");
}

这篇关于在RequestScoped bean CDI上的HttpServletRequest注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆