从托管bean中添加构造函数的官方教程中考虑基于JSF的web-app hello1
的示例。关注index.xhtml
facelet
<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>
和modidfied托管bean Hello.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
点击后有回发请求。因此,正如在tutroial中所写,我们有以下执行阶段的子阶段:
将创建托管bean的哪个阶段实例?
为此实例创建调用什么构造函数以及为什么?我不明白如何从index.xhtml
代码中观察到它。
答案 0 :(得分:3)
将创建托管bean的哪个阶段实例?
没有人具体。当第一次任意EL表达式需要引用托管bean时,它是第一次构造,而bean实例在其范围内不存在。这不依赖于任何特定的面部事件。这可以在恢复视图阶段(第一阶段)期间,但在渲染响应阶段(最后阶段)或其间的任何其他阶段也可以这样。
这一切都取决于在EL上下文中通过#{bean.xxx}
在视图中(或在模型中以编程方式)引用bean的方式和位置。你通常不应该担心这一点。 JSF(特别是EL)至少不会为了正确构建或处理或渲染视图而尽快构建它。
将为此实例创建调用什么构造函数以及为什么?
默认构造函数,当然。因为Javabeans规范是这样说的。所有其他构造函数从未被JSF / CDI托管bean工具使用。
即便如此,你也不应该担心施工人员。您最好在@PostConstruct
带注释的方法中而不是在构造函数中执行初始化。也就是说,当bean由使用代理(例如CDI)的bean管理框架管理时,默认构造函数可能比预期更频繁地被调用。
我不明白如何从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
等等。