简体   繁体   English

EJB 3.1-注入会话范围Pojo

[英]EJB 3.1 - Inject Session Scope Pojo

Using: EJB 3.1, JBoss AS 7, RestEasy. 使用:EJB 3.1,JBoss AS 7,RestEasy。

I have a session scoped bean which I want to user to store user informations for the session. 我有一个会话范围的bean,我想让用户存储该会话的用户信息。

import java.io.Serializable;
import javax.enterprise.context.SessionScoped
@SessionScoped
public class LoggedInUser implements Serializable {
    private String id;
    ...
}

If the user open my web application a filter extract header informations (application runs behind a webseal) which contains a user identification. 如果用户打开我的Web应用程序,则过滤器会提取包含用户标识的标头信息(应用程序在WebSeal后面运行)。 I need to create a logged in user object (see LoggedInUser above) there (after calling ldap). 我需要在那里(调用ldap之后)创建一个已登录的用户对象(请参见上面的LoggedInUser)。 Afterwards I want to inject this LoggedInUser object in different @Stateless Beans, but LoggedInUser is always "empty" (members are null). 之后,我想将此LoggedInUser对象注入到不同的@Stateless Bean中,但是LoggedInUser始终为“空”(成员为null)。

Inject sample: 进样:

@Path("/country")
@Stateless
public class CountryController extends AbstractController {
@Inject
private Logger LOGGER;
@Inject
private LoggedInUser loggedInUser;
//@Inject dont work too..
//private Instance<LoggedInUser> loggedInstance

What do Im wrong? 我错了什么?

Judging by the comment and the question - you need some help with EJB concepts. 从注释和问题来看-您需要EJB概念方面的帮助。

@SessionScoped beans are beans that have one instance per session. @SessionScoped bean是每个会话具有一个实例的bean。 Every @Inject with bean like that during one session will reference the same object. 在一个会话期间,每个具有bean的@Inject都将引用同一对象。 That's it however - if you want your bean to contain some information specific to that session, you will need to put it there yourself, using setters or other methods like you would do in every normal Java class. 就是这样-如果您希望bean包含特定于该会话的某些信息,则需要像使用每个普通Java类一样,使用setter或其他方法将其自己放在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM