cdi观察会话scoped bean改变了

时间:2017-05-26 00:48:18

标签: java cdi observable

我正在尝试在更改任何属性后观察@SessionScoped组件。 HttpSessionAttributeListener不会触发cdi托管组件中的更改。

@SuppressWarnings("serial")
@SessionScoped
public class TestSession implements Serializable {


private User user;

public TestSession() {

}

public User getUser() {
    return user;
}

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

}

示例Servlet:

@SuppressWarnings("serial")
@WebServlet(name = "demo", urlPatterns = "/demo")
public class DemoServlet extends HttpServlet {

private static final Logger logger = LoggerFactory.getLogger(DemoServlet.class);

@Inject
private TestSession testSession;

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {

    User user = new User(1L,new Role(1L));
    user.setId(RandomUtils.nextLong());
    testSession.setUser(user); //listen that component change something


    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/demo.jsp");
    dispatcher.forward(httpServletRequest, httpServletResponse);
}

}

组件更改任何属性时是否可以监听?有人有办法做到这一点吗?

重要提示:我无权访问TestSession java类或servlet上的重写或添加代码。

1 个答案:

答案 0 :(得分:0)

您可以在创建interceptor的setUser()方法中放置event,然后抓住它。