在Glassfish中没有调用@PostConstruct

时间:2015-02-28 12:39:48

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

我有这样的依赖:

<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();
        } 
    }

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

1 个答案:

答案 0 :(得分:-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上运行。 附:我仍然不完全确定为什么两个wariants都在使用Tomcat。

相关问题