【问题标题】:@PostConstruct not called in Glassfish在 Glassfish 中未调用 @PostConstruct
【发布时间】:2015-05-01 03:40:08
【问题描述】:

我有这样的依赖:

<dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.10</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.10</version>
    </dependency>

和托管bean:

@ManagedBean(eager = true)
@ApplicationScoped
public class AppBean implements Serializable{
    private List<SelectItem> someEntitySI = null;

    @PostConstruct
    public void init(){
       try {
            someEntitySI = new ArrayList<SelectItem>();
            List<SomeEntity> types = Factory.getInstance().getSomeEntityDAO().getAllSomeEntities();
            for(SomeEntity type : types) {
                someEntitySI .add(new SelectItem(someEntity.getId(), someEntity.getName()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } 
    }

当我在调用 @PostConstruct 的 Tomcat 上部署它时,但是当我在 Glassfish(v4.1) 上部署相同的代码时,@PostConstruct 不起作用。为什么会这样?

【问题讨论】:

  • 人不是读心术。您需要指定“它有效”的含义并描述导致您得出“无效”结论的结果。

标签: tomcat jsf-2 glassfish-4 java-ee-7


【解决方案1】:

如果你和我一样笨,你的 web.xml 开头是这样的:

<web-app>
 ....
</web-app>

那么你需要编辑它:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
     ....
     </web-app>

@PostConstrcut 注释将在 Glassfish 上工作。 附言我仍然不完全确定为什么这两个 wariant 都在 Tomcat 上工作。

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多