[SerializeField] private float cur_HP;
private const float max_HP = 100;
private Vector3 PrevmousePos;
private bool SpaceIsPressed;
// Start is called before the first frame update
void Start()
{
cur_HP = max_HP;
}
// Update is called once per frame
void Update()
{
SpaceIsPressed = Input.GetKey(KeyCode.Space);
Wheel();
}
void Wheel()
{
Vector3 mouseDelta = Input.mousePosition - PrevmousePos;
if (mouseDelta.x > 0 && SpaceIsPressed)
{
float amount = 0.01f;
cur_HP -= mouseDelta.x * amount;
} else if(mouseDelta.x < 0 && SpaceIsPressed)
{
float amount = 0.01f;
cur_HP += mouseDelta.x * amount;
}
if (cur_HP <= 0)
{
cur_HP = 0;
Debug.Log("You have unlocked");
}
if (cur_HP > 100)
{
cur_HP = 100;
}
transform.localRotation = Quaternion.Euler(0, 0, (720 / max_HP * cur_HP));
PrevmousePos = Input.mousePosition;
}
已被弃用 ,因此我使用recommended CDI替换 @ManagedBean
及其@Named
注解(不是JSF javax.faces.view.ViewScoped
一个)。
问题
Bean表现为javax.faces.bean.ViewScoped
,即@ApplicationScoped
正在在应用部署时被调用,然后再也不会被调用,即我看不到{页面访问/刷新上的{1}}调用。
如果我切换回JSF注释,即@PostConstruct
+ @PostConstruct
bean 可以正常工作。
我也尝试过How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1中列出的@ManagedBean
+ javax.faces.bean.ViewScoped
,但结果与@Named
(OmniFaces 2.6.2)相同。
长篇故事
这是一个JSF + Spring集成问题(4.3.1.RELEASE)。
详细信息
应用程序在 Wildfly 11.0.0 上运行。
Bean
org.omnifaces.cdi.ViewScoped
查看(PrimeFaces 6.0.18)
javax.faces.view.ViewScoped
编辑
在Kukeltje的建议之后,我研究了bean的创建调用堆栈。 Spring以某种方式管理bean的创建。如果我从import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
@Named
@ViewScoped
public class TestBean implements Serializable {
private String text;
@PostConstruct
private void onPostConstruct() { /* ... */ }
public void init() { /* ... */ }
public void onCommandButton() { /* ... */ }
public String getText() { return text; }
public void setText(String text) { this.text = text; }
public String getOutput() { return text; }
}
构造函数中抛出异常,则这是一个堆栈跟踪。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<f:event type="preRenderComponent" listener="#{testBean.init}"/>
<h:form id="form">
<p:inputText id="text" value="#{testBean.text}"/>
<h:outputText id="output" value="#{testBean.output}" />
<p:commandButton value="SUBMIT"
action="#{testBean.onCommandButton}"
update="output"/>
</h:form>
</h:body>
</html>
我检查了配置,发现注册了用于项目bean(TestBean
+ Constructor threw exception; nested exception is java.lang.RuntimeException: TestBean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:205)
at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:174)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:239)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:99)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Constructor threw exception; nested exception is java.lang.RuntimeException: TestBean
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:159)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 33 more
的自定义Spring @Scope("view")
实现(类似于this,但没有回调处理) -可能是错误的设计)。我认为这与给定的问题无关,但出于完整性的考虑,@Controller
的样子如下:
@Scope("view")
这可能是问题的根源
applicationContext.xhtml
看来,Spring的CDI与应用程序的CDI冲突。您有什么建议吗?