3

考虑官方教程中基于 JSF 的 web-app 示例,hello1在托管 bean 中添加了构造函数。跟随index.xhtml小脸

<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelets Hello Greeting</title>
    </h:head>
    <h:body>
        <h:form>
            <h:graphicImage url="#{resource['images:duke.waving.gif']}" 
                            alt="Duke waving his hand"/>
            <h2>Hello hui, my name is Duke. What's yours?</h2>
            <h:inputText id="username"
                         title="My name is: "
                         value="#{hello.name}"
                         required="true"
                         requiredMessage="Error: A name is required."
                         maxlength="25" />
            <p></p>
            <h:commandButton id="submit" value="Submit" action="response">
            </h:commandButton>
            <h:commandButton id="reset" value="Reset" type="reset">
            </h:commandButton>
        </h:form>
        <div class="messagecolor">
            <h:messages showSummary="true" 
                        showDetail="false"
                        errorStyle="color: #d20005" 
                        infoStyle="color: blue"/>
        </div>
    </h:body>
</html>

和修改的托管beanHello.java

package javaeetutorial.hello1;


import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class Hello {

    private String name;

    public Hello() {
    }
    public Hello(String name){
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String user_name) {
        this.name = user_name;
    }
}

两个公共构造函数。让我们在服务器上部署这个应用程序并发送初始请求,输入名称inputText并单击submit。点击后有回发请求submit。因此,如教程中所写,我们有以下执行阶段的子阶段:

  1. 应用程序视图被构建或恢复。
  2. 应用请求参数值。
  3. 对组件值执行转换和验证。
  4. 托管 bean 使用组件值进行更新。
  5. 调用应用程序逻辑。

在什么阶段创建托管 bean 的实例?

此实例创建将调用什么构造函数,为什么?我不明白如何从index.xhtml代码中观察到它。

4

1 回答 1

3

在什么阶段创建托管 bean 的实例?

具体没有人。当任意 EL 表达式第一次需要引用托管 bean 而 bean 实例不存在于其范围内时,它是第一次构造的。这不依赖于任何特定的面孔事件。这可以在恢复视图阶段(第一阶段),但也可以在渲染响应阶段(最后一个阶段)或介于两者之间的任何其他阶段。

这一切都取决于通过#{bean.xxx}视图(或以编程方式在模型中)在 EL 上下文中引用 bean 的方式和位置。您通常不应该担心这一点。JSF(特别是 EL)至少不会为了正确构建或处理或呈现视图而提前构建它。


此实例创建将调用什么构造函数,为什么?

当然是默认构造函数。因为 Javabeans 规范是这么说的。JSF/CDI 托管 bean 工具从不使用所有其他构造函数。

即使那样,您也不应该担心构造函数。您最好在带@PostConstruct注释的方法中执行初始化,而不是在构造函数中。即,当 bean 由使用代理的 bean 管理框架(例如 CDI)管理时,默认构造函数的调用可能比期望的更频繁。


我不明白如何从 index.xhtml 代码中观察到它。

只需在构造函数@PostConstruct或任何相关的 getter/setter 方法中放置一个断点,然后在调试模式下运行项目。一旦断点命中,检查调用堆栈。所涉及的类和方法通常具有相当自记录的名称。这是一个示例,当您使用时调用堆栈的外观@Named

Daemon Thread [http-bio-8088-exec-6] (Suspended (entry into method <init> in TestBean)) 
    owns: LocalCache$StrongEntry  (id=503)  
    owns: SocketWrapper  (id=504)   
    TestBean$Proxy$_$$_WeldClientProxy.<init>() line: not available [local variables unavailable]   
    NativeConstructorAccessorImpl.newInstance0(Constructor, Object[]) line: not available [native method]   
    NativeConstructorAccessorImpl.newInstance(Object[]) line: 57    
    DelegatingConstructorAccessorImpl.newInstance(Object[]) line: 45    
    Constructor.newInstance(Object...) line: 526    
    Class.newInstance() line: 374   
    NewInstanceAction.run() line: 33    
    AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
    ClientProxyFactory(ProxyFactory).create(BeanInstance) line: 271 
    ClientProxyFactory.create(BeanInstance) line: 111   
    ClientProxyProvider.createClientProxy(Bean<T>, Set<Type>) line: 181 
    ClientProxyProvider.createClientProxy(Bean<T>) line: 171    
    ClientProxyProvider.access$100(ClientProxyProvider, Bean) line: 45  
    ClientProxyProvider$CreateClientProxy.load(Bean<Object>) line: 56   
    ClientProxyProvider$CreateClientProxy.load(Object) line: 52 
    LocalCache$LoadingValueReference.loadFuture(K, CacheLoader<? super K,V>) line: 3589 
    LocalCache$Segment.loadSync(K, int, LoadingValueReference<K,V>, CacheLoader<? super K,V>) line: 2374    
    LocalCache$Segment.lockedGetOrLoad(K, int, CacheLoader<? super K,V>) line: 2337 
    LocalCache$Segment.get(K, int, CacheLoader<? super K,V>) line: 2252 
    LocalCache.get(K, CacheLoader<? super K,V>) line: 3990  
    LocalCache.getOrLoad(K) line: 3994  
    LocalCache$LocalLoadingCache.get(K) line: 4878  
    LoadingCacheUtils.getCacheValue(LoadingCache<K,V>, K) line: 52  
    LoadingCacheUtils.getCastCacheValue(LoadingCache<K,V>, Object) line: 80 
    ClientProxyProvider.getClientProxy(Bean<T>) line: 187   
    WeldELResolver(AbstractWeldELResolver).lookup(BeanManagerImpl, ELContext, String) line: 110 
    WeldELResolver(AbstractWeldELResolver).getValue(ELContext, Object, Object) line: 91 
    WeldApplication$LazyBeanManagerIntegrationELResolver(ForwardingELResolver).getValue(ELContext, Object, Object) line: 49 
    CompositeELResolver.getValue(ELContext, Object, Object) line: 67    
    DemuxCompositeELResolver._getValue(int, ELResolver[], ELContext, Object, Object) line: 176  
    DemuxCompositeELResolver.getValue(ELContext, Object, Object) line: 203  
    AstIdentifier.getValue(EvaluationContext) line: 72  
    ValueExpressionImpl.getValue(ELContext) line: 185   
    WeldValueExpression.getValue(ELContext) line: 50    
    ELText$ELTextVariable.writeText(ResponseWriter, ELContext) line: 227    
    ELText$ELTextComposite.writeText(ResponseWriter, ELContext) line: 150   
    TextInstruction.write(FacesContext) line: 85    
    UIInstructions.encodeBegin(FacesContext) line: 82   
    UIInstructions(UILeaf).encodeAll(FacesContext) line: 207    
    HtmlBody(UIComponent).encodeAll(FacesContext) line: 1899    
    UIViewRoot(UIComponent).encodeAll(FacesContext) line: 1899  
    FaceletViewHandlingStrategy.renderView(FacesContext, UIViewRoot) line: 451  
    MultiViewHandler.renderView(FacesContext, UIViewRoot) line: 131 
    ConversationAwareViewHandler(ViewHandlerWrapper).renderView(FacesContext, UIViewRoot) line: 337 
    RenderResponsePhase.execute(FacesContext) line: 120 
    RenderResponsePhase(Phase).doPhase(FacesContext, Lifecycle, ListIterator<PhaseListener>) line: 101  
    LifecycleImpl.render(FacesContext) line: 219    
    FacesServlet.service(ServletRequest, ServletResponse) line: 647 
    ...

从底部开始(我已经删除了之后的所有行,FacesServlet.service因为这些行通常无关紧要)并从下到上阅读。RenderResponsePhase.execute告诉它在渲染响应阶段执行。TextInstruction.write告诉它在模板文本中写入 EL 的结果时发生,如下所示<p>#{bean.something}</p>。剩下的就是 CDI 实现 Weld 如何查找和实例化代理,以及它如何反过来实例化实际的 bean 引用。

如果它发生在不同的阶段,您将不会RenderResponsePhase.execute看到例如UpdateModelValuesPhase.execute等等。

于 2013-11-19T09:59:07.643 回答