托管 bean 在哪个阶段构建以及使用哪个构造函数
-
21-12-2019 - |
题
考虑基于 JSF 的 Web 应用程序的示例 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>
和修改后的托管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
点击。因此,正如教程中所写,我们有以下执行阶段的子阶段:
- 构建或恢复应用程序视图。
- 应用请求参数值。
- 对组件值执行转换和验证。
- 托管 Bean 使用组件值进行更新。
- 调用应用程序逻辑。
将在哪个阶段创建托管 bean 实例?
创建此实例时将调用哪个构造函数?为什么?我不明白如何从 index.xhtml
代码。
解决方案
将在哪个阶段创建托管 bean 实例?
没有具体的人。当任意 EL 表达式需要第一次引用托管 bean,而 bean 实例不存在于其范围内时,它是第一次构造的。这不依赖于任何特定的面孔事件。这可以在恢复视图阶段(第一阶段)期间进行,但这也可以在渲染响应阶段(最后阶段)或之间的任何其他阶段期间进行。
这一切都取决于 EL 上下文中如何以及在何处引用 bean #{bean.xxx}
在视图中(或以编程方式在模型中)。您通常不必担心这一点。JSF(特别是 EL)至少不会为了正确构建、处理或渲染视图而提前构建它。
创建此实例时将调用哪个构造函数?为什么?
当然是默认构造函数。因为 Javabean 规范是这么说的。JSF/CDI 托管 bean 工具从不使用所有其他构造函数。
即使如此,您也不应该担心构造函数。你最好在a中执行初始化 @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
等等。