标签: postconstruct

何时使用f:viewAction/preRenderView与PostConstruct?

什么时候应该使用f:viewActionor preRenderView事件来初始化页面的数据而不是使用@PostConstruct注释?基于支持bean的范围类型使用一个或另一个的基本原理例如,如果支持bean是@RequestScoped,那么在呈现视图之前选择使用f:viewActionpreRenderView覆盖@PostConstruct初始化支持bean是不相关的,因为两者会结果是一样的吗?

f:viewAction或preRenderView

<f:metadata>
  <f:viewAction action="#{myBean.initialize}" />
</f:metadata>
Run Code Online (Sandbox Code Playgroud)
<f:metadata>
  <f:event type="preRenderView" listener="#{myBean.initialize}"/>
</f:metadata>
Run Code Online (Sandbox Code Playgroud)

要么

@PostConstruct

public class MyBean
{
    @PostConstruct
    public void initialize()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

jsf postconstruct jsf-2 prerenderview viewaction

90
推荐指数
1
解决办法
7万
查看次数

在即时化对象后,Guice调用init方法

是否有可能告诉Guice在定时给定类型的对象后调用某个方法(即init())?

我在EJB 3中寻找类似于@PostConstruct注释的功能.

init guice postconstruct

62
推荐指数
4
解决办法
3万
查看次数

为什么@PostConstruct回调每次都会触发,即使bean是@ViewScoped?JSF

我在页面上使用datatable并使用绑定属性将其绑定到我的支持bean.这是我的代码: -

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
            <h:form prependId="false">

                <h:dataTable var="item" value="#{testBean.stringCollection}" binding="#{testBean.dataTable}">
                    <h:column>
                        <h:outputText value="#{item}"/>
                    </h:column>
                    <h:column>
                        <h:commandButton value="Click" actionListener="#{testBean.action}"/>
                    </h:column>
                </h:dataTable>

            </h:form>

    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我的豆子: -

package managedBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlDataTable;

@ManagedBean(name="testBean")
@ViewScoped
public class testBean implements Serializable {

    private List<String> stringCollection;

    public List<String> getStringCollection() {
        return stringCollection;
    }

    public void …
Run Code Online (Sandbox Code Playgroud)

jsf facelets postconstruct view-scope

32
推荐指数
1
解决办法
2万
查看次数

@PostConstruct&Checked例外

@PostConstruct文档中,它描述了带注释的方法:

"该方法绝不能抛出已检查的异常."

如何处理例如可以在这种方法中抛出的IOException?只需将其包装在RuntimeException中,让用户担心对象的错误初始状态?或者@PostConstruct是错误的地方来验证和初始化注入了依赖项的对象?

java exception-handling java-ee postconstruct

32
推荐指数
2
解决办法
2万
查看次数

@ViewScoped在每个回发请求上调用@PostConstruct

这似乎不对.我正在清理我的代码,我只是注意到了这一点.每个ajax请求都会触发构造函数和@PostConstruct我的@ViewScopedbean.即使是简单的数据库分页也会触发它.

知道@ViewScoped@RequestScoped任何请求都要重建并且不应该重建.只有在通过GET重新加载完整页面之后.

jsf constructor postconstruct jsf-2 view-scope

28
推荐指数
1
解决办法
2万
查看次数

如何使用Spring测试具有@PostConstruct方法的类的构造函数?

如果我有一个带有@PostConstruct方法的类,我如何使用JUnit和Spring测试其构造函数及其@PostConstruct方法?我不能简单地使用新的ClassName(param,param),因为它不使用Spring - @PostConstruct方法没有被触发.

我错过了一些明显的东西吗?

public class Connection {

private String x1;
private String x2;

public Connection(String x1, String x2) {
this.x1 = x1;
this.x2 = x2;
}

@PostConstruct
public void init() {
x1 = "arf arf arf"
}

}


@Test
public void test() {
Connection c = new Connection("dog", "ruff");
assertEquals("arf arf arf", c.getX1();
}
Run Code Online (Sandbox Code Playgroud)

我有类似的东西(虽然稍微复杂一些)并且@PostConstruct方法没有被击中.

java junit spring unit-testing postconstruct

24
推荐指数
2
解决办法
3万
查看次数

当使用构造函数参数自动装配原型bean时,为什么不调用@PostConstruct方法

我有一个原型范围bean,我希望通过@Autowired注释注入它.在这个bean中,还有@PostConstruct方法,Spring没有调用它,我不明白为什么.

我的bean定义:

package somepackage;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
@Scope("prototype")
public class SomeBean {

    public SomeBean(String arg) {
        System.out.println("Constructor called, arg: " + arg);
    }

    @PostConstruct
    private void init() {
        System.out.println("Post construct called");
    }

}
Run Code Online (Sandbox Code Playgroud)

我想要注入bean的JUnit类:

package somepackage;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath*:applicationContext-test.xml")
public class SomeBeanTest {

    @Autowired
    ApplicationContext ctx;

    @Autowired
    @Value("1")
    private SomeBean someBean;

    private SomeBean someBean2;

    @Before
    public void setUp() throws Exception { …
Run Code Online (Sandbox Code Playgroud)

java spring postconstruct prototype-scope

20
推荐指数
2
解决办法
4339
查看次数

如何在@PostConstruct期间添加Faces消息

在一个支持bean的@PostConstruct方法中,我调用了一个EJB,它可能会返回一些我希望通过p:messages在页面上显示的消息.但是,即使我添加FacesMessages,例如FacesContext.getCurrentInstance().addMessage(...),也不会使用FacesMessages更新p:messages.

如果我不是调用从页面的动作调用EJB(比如用户点击它调用调用EJB的方法在页面上的一个按钮,然后添加的FacesMessage(S)),那么messags显示用对:消息按预期.

如何在@PostConstruct期间添加Faces消息并在最初呈现页面时显示它们?

码:

Page1Controller.java:

@ManagedBean
public class Page1Controller
{
    @PostConstruct
    public void init()
    {
        FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage("Test Message from @PostConstruct"));
    }

    public String getValue()
    {
            return "Some Value";
    }

    public void triggerMessage(ActionEvent event)
    {
            FacesContext.getCurrentInstance().addMessage(null, 
                    new FacesMessage("Test Message from Trigger Button"));      
    }

}
Run Code Online (Sandbox Code Playgroud)

page1.xhtml

   <h:form>
        <p:messages showDetail="true" showSummary="true" autoUpdate="true"/>
        <h:outputText value="#{page1Controller.value}"/>
        <br/>
        <p:commandButton value="Trigger Message" 
                         actionListener="#{page1Controller.triggerMessage}"/>  
   </h:form>
Run Code Online (Sandbox Code Playgroud)

jsf message pageload postconstruct

19
推荐指数
1
解决办法
1万
查看次数

@PostConstruct方法为同一请求调用两次

我正在使用JSF 2.0和GlassFish 3.0.

我有以下Managed Bean:

@ManagedBean
@RequestScoped
public class OverviewController{

    private List<Event> eventList;

    @PostConstruct
    public void init(){
        System.out.println("=> OverviewController - init() - enter");

        System.out.println("=< OverviewController - init() - exit");
    }
}
Run Code Online (Sandbox Code Playgroud)

overview.xhtml文件中,我从OverviewController中调用了不同的属性或方法.

<ui:repeat var="event" value="#{overviewController.eventList}">
    ...
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)

一切正常,但问题出在日志文件中:

INFO: Enter : RESTORE_VIEW 1
INFO: Exit : RESTORE_VIEW 1

INFO: Enter : RENDER_RESPONSE 6
INFO: => OverviewController - init() - enter
INFO: => Overview Controller - updateSelectedTab() - enter
INFO: =< Overview Controller - updateSelectedTab() - exit
INFO: =< …
Run Code Online (Sandbox Code Playgroud)

java jsf postconstruct jsf-2

17
推荐指数
1
解决办法
3万
查看次数

多个PostConstruct方法?

它在Java的 PostConstruct 文档页面中说明了这一点

只有一个方法可以使用此注释进行注释

但我只是尝试用PostConstruct注释一个独立应用程序的三种方法.没有编译错误,并且所有三个都被调用并顺利执行.

那我错过了什么?什么样的类可以存在多个PostConstruct注释?

java javadoc postconstruct

16
推荐指数
2
解决办法
6462
查看次数