我试图在已废弃的ManagedBean/ManagedProperty注释上使用CDI,并在一个非常简单的web应用程序中遇到这个异常:
Error creating bean with name 'navigationController': Unsatisfied dependency expressed through field 'message'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject(), @org.omnifaces.cdi.Param(validatorAttributes=[], validatorMessage=, validators=[], converter=, pathIndex=-1, converterAttributes=[], converterClass=interface javax.faces.convert.Converter, label=, overrideGlobalBeanValidationDisabled=false, required=false, disableBeanValidation=false, name=, validatorClasses=[], converterMessage=, requiredMessage=)}我试图效仿OmniFaces展示http://showcase.omnifaces.org/cdi/Param中的@Param。
我用http://localhost:8080/jsfSpringBootApp/nav.xhtml?message=My+message+from+MessageSource写了一页。我的理解是,应该在指向NavigationController的导航上创建nav.xhtml bean,并且消息字段将使用从request参数获取的值填充。
IntellliJ还抱怨@Param注释:
无法找到具有@Param限定的bean
谢谢你的帮助。我被困在接下来该尝试什么了。
整个项目都在maffitt/jsfspringbootapp.git。
nav.xhtml的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body >
<f:view>
<p:panel id="myPanelId" header="My Panel" style="margin-bottom:20px">
<h:outputText value="My text output." />
<h:outputText value="My text output from params: #{navigationController.action}" />
</p:panel>
</f:view>
</h:body>
</html>NavigationController.java的内容为包装org.nrg.cap.jsfWebApp;
import org.omnifaces.cdi.Param;
import javax.enterprise.context.RequestScoped;
import javax.faces.annotation.ManagedProperty;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
//@Component
//@Scope("request")
@Named
@RequestScoped
public class NavigationController implements Serializable {
@Inject @Param
private String message;
public String showPage() {
return ("fubar".equals(message))? "fubar": "snafu";
}
public void setAction(String message) {
this.message = message;
}
public String getAction() {
return message;
}
}pom.xml是
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.nrg.cap</groupId>
<artifactId>jsfSpringBootApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JSF Spring Boot WebApp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<joinfaces.version>4.0.0</joinfaces.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>joinfaces-dependencies</artifactId>
<version>${joinfaces.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>primefaces-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>omnifaces3-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0.SP1</version>
</dependency>
</dependencies>
</project>我发现我必须添加cdi的依赖项,因为我正在部署到tomcat 9.0.13,它没有cdi。pom是从joinfaces项目中派生出来的。
发布于 2018-12-17 11:37:47
OmniFaces @Param注释是一个基于CDI (实现)的注释。虽然Spring可以使用@Named和@Inject代替@Component和@Autowired,就像在What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?中提到的那样(确实需要添加一个‘CDI’‘api’),但它仍然不能使Spring成为真正的CDI容器。
这意味着@Param不会像OmniFaces/CDI所期望的那样被Spring使用/解释,而Spring试图用@Param注释找到一个真实的bean,而这个注释当然不存在。因此,你得到了你得到的错误。
最好的解决方案是,超出了这个问题的范围,因为它主要是基于意见的,但基本上可以归结为两种选择。
我不会说第一个有我的偏好.嗯,刚刚.
发布于 2018-12-16 18:01:58
您应该更改bean.xml set bean-discovery-mode="all"> https://github.com/armdev/reactive-javaserver-faces/blob/master/reactive-jsf/src/main/webapp/WEB-INF/beans.xml
发布于 2018-12-16 21:10:59
我更深入地研究:基于Joinfaces的项目,它意味着项目的基础是Spring引导。所以,在使用CDI作为DI之后。将CDI注解更改为Spring @Inject to @Autowired。将两个不同的DI原则结合起来不是个好主意。
https://stackoverflow.com/questions/53795984
复制相似问题