JSF应用程序范围实例化和注入 [英] JSF application scope instantiation and injection

查看:132
本文介绍了JSF应用程序范围实例化和注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能我的问题很简单,但是我以前从未使用过应用程序范围bean.我需要应用程序Bean,因为我必须在数据库上进行耗时的事务. 我的搜索根本无法满足我的好奇心. 我不知道为什么,但是我没有设法初始化bean(它为null),否则应用程序崩溃了. 所以我有一个应用范围bean

Probably my question is a trivial one, but I never used an application scope bean before. I need the application bean because I have to do time consuming transactions on the database. my search didn't satisfy my curiosity at all. I don't know why but I didn't manage to initialize the bean (it is null) or it the app crashed. So I have an application scope bean

@ManagedBean(eager=true)
@ApplicationScoped
public class ApplicationContainer {
...
}

eager = true,我读到它告诉JSF每次启动应用程序服务器(我使用GlassFish)时都要启动Bean.

eager=true I read that tells JSF to initiate the bean every time when the application server (I use GlassFish) is started.

我在几个地方读到了我只需要添加此批注,即可初始化Bean. 对我来说不是 读完之后,如果我想将应用程序bean注入另一个bean中,则必须使用@PostConstuct批注

I read in several places that I just have to put this annotation and the bean gets initialized. For me it doesn't... After I read that if I want to inject the application bean into another bean I have to use @PostConstuct annotation

@ManagedBean
@SessionScoped
public class TestsBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @ManagedProperty(value = "#{container}")
    private ApplicationContainer container;

    @PostConstruct
    public void init() {
    container.contructContainer();
    }

这会给我将TestsBean注入...的其他bean产生错误.

this gives an error in other bean that I inject the TestsBean into...

  • 如果服务器启动时应用程序bean被初始化,它会在应用程序bean的主体中调用什么方法来执行所需的操作?还是在注入的bean中通过后构造方法完成?

请告诉我处理应用程序bean的正确方法.我真的很困惑...

Please tell me the proper way to handle application beans. I an really confused...

谢谢大家的光临!

推荐答案

有两个潜在的错误.

首先,@ManagedBean(eager=true)的工作方式为其javadoc 说,仅在应用程序范围内的JSF托管bean上.因此,仅当您使用过javax.faces.bean软件包的@ApplicationScoped(因此不是javax.enterprise.context软件包!)时,它才起作用. eager=true基本上意味着该bean将在webapp启动时自动实例化,而不是仅在EL中首次引用该bean时才自动实例化.

First, the @ManagedBean(eager=true) works, as its javadoc says, only on application scoped JSF managed beans. So it works only when you have used @ApplicationScoped of javax.faces.bean package (and thus not javax.enterprise.context package!). The eager=true basically means that the bean will be auto-instantiated on webapp's startup instead of only later when it's referenced for the first time in EL.

第二,根据Javabeans规范,托管Bean名称默认为大写形式的类名称.您没有显式指定任何托管bean名称,例如@ManagedBean(name="container", eager=true),因此托管bean名称将默认为applicationContainer,但是您仍在尝试将其引用为#{container}而不是#{applicationContainer}.

Second, the managed bean name defaults to the classname in decapitalized form according the Javabeans specification. You didn't explicitly specify any managed bean name like @ManagedBean(name="container", eager=true), so the managed bean name will default to applicationContainer, however yet you're still attempting to reference it as #{container} instead of #{applicationContainer}.

您根本不清楚所面临的问题/错误.如果您遇到异常,则应该绝对地阅读/解释它,如果您无法理解它,请完整地复制粘贴它-包括问题中的stacktrace-.它即代表了您问题的全部答案.您只需要解释和理解它(或者我们只需要用外行的术语来解释它)即可.您实际上不应该忽略它们,而不要将它们当作无关紧要的装饰摆在问题之外.他们不是!

You are not clear at all on which problems/errors you're facing. If you're getting an exception, you should absolutely read/interpret it and if you're unable to understand it, copypaste it in its entirety -including the stacktrace- in the question. It represents namely the whole answer to your problem at its own. You just have to interpret and understand it (or we just have to explain it in layman's terms). You should really not ignore them and leave them out the question as if they are irrelevant decoration. They are not!

总而言之,一个完整而正确的方法就是,确保导入声明完整,还有一些穷人的标准输出进行调试:

All with all, the complete and proper approach would be, complete with the import declarations just to be sure, and also some poor man's stdout prints for debugging:

package com.example;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean(eager=true)
@ApplicationScoped
public class ApplicationContainer {

    public ApplicationContainer() {
        System.out.println("ApplicationContainer constructed");
    }

}

package com.example;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class TestsBean implements Serializable {

    @ManagedProperty("#{applicationContainer}")
    private ApplicationContainer container;

    public TestsBean() {
        System.out.println("TestsBean constructed");
    }

    @PostConstruct
    public void init() {
        System.out.println("ApplicationContainer injected: " + container);
    }

    public void setContainer(ApplicationContainer container) {
        this.container = container;
    }

}

这篇关于JSF应用程序范围实例化和注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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