JSF 2.3 与 Spring 4.3 @Inject @ManagedProperty 问题

Posted

技术标签:

【中文标题】JSF 2.3 与 Spring 4.3 @Inject @ManagedProperty 问题【英文标题】:JSF 2.3 with Spring 4.3 @Inject @ManagedProperty issue 【发布时间】:2017-09-14 18:59:09 【问题描述】:

我将Faces-Config.xml 设置为web-facesconfig_2_3.xsd。获得了 JSF 2.3 的最新 jars。使用 JSF 和 Spring 的 CDI 注释,创建了两个具有请求范围的 bean,并尝试从另一个 bean paymentBean 注入托管属性。 关注了这个link。

import javax.inject.Inject;
import javax.inject.Named;
import javax.faces.annotation.ManagedProperty;

@Named("myBean")
@RequestScoped
public class MyBean implements Serializable 

    @Inject @ManagedProperty("#paymentBean.amount")
    private BigDecimal amount;



@Named(value = "paymentBean")
@RequestScoped
public class paymentBean implements Serializable 

    private BigDecimal amount;


它抛出以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:592)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    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:754)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    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.deploy(DeploymentManagerImpl.java:198)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)
    ... 25 more
15:03:12,496 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 81) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./VTOL: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./VTOL: java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:231)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    ... 6 more
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:592)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
    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:754)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    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.deploy(DeploymentManagerImpl.java:198)
    ... 8 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.amount)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589)
    ... 25 more

15:03:12,506 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "VTOL.war")]) - failure description: "WFLYCTL0080: Failed services" => "jboss.undertow.deployment.default-server.default-host./VTOL" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./VTOL: java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    Caused by: java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.payment.amount)
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.amount)
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.amount)"
15:03:12,536 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "VTOL.war" (runtime-name : "VTOL.war")
15:03:12,536 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.undertow.deployment.default-server.default-host./VTOL: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./VTOL: java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyBean': Unsatisfied dependency expressed through field 'amount'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.math.BigDecimal' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @javax.inject.Inject(), @javax.faces.annotation.ManagedProperty(value=#paymentBean.amount)

【问题讨论】:

【参考方案1】:

在payara下,我发现用

配置了faces
<faces-config version="2.3"

不工作,

我用过

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

</faces-config>

所有问题都解决了。 尝试这个 https://github.com/armdev/jsf-rulebook/blob/master/web-flow/src/main/webapp/WEB-INF/faces-config.xml

第一次尝试在没有 Spring 的情况下运行项目,如果一切正常,请集成您喜欢的框架。

【讨论】:

以上是关于JSF 2.3 与 Spring 4.3 @Inject @ManagedProperty 问题的主要内容,如果未能解决你的问题,请参考以下文章

JSF 与 Spring Security 的集成

JSF 2.3 中的 f:websocket

JSF-2.3 找不到我的 @Named CDI-1.2 托管 bean

JSF 2.3 无法通过 Tomcat 9 支持 WebSocket

JSF 2.3 基于表单的登录和 ViewExpiredException

Spring JSF 集成:如何在 JSF 托管 bean 中注入 Spring 组件/服务?