gpt4 book ai didi

spring - CDI @ViewScoped bean 功能不可用

转载 作者:行者123 更新时间:2023-12-03 23:17:40 24 4
gpt4 key购买 nike

我将 JSF 2.2.14 与 Spring Boot 1.4.4 一起使用,我定义了一个自定义 View 范围,如下所示:

public class FacesViewScope implements Scope {

public static final String NAME = "view";

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
throw new IllegalStateException("FacesContext.getCurrentInstance() returned null");
}

Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

if (viewMap.containsKey(name)) {
return viewMap.get(name);
} else {
Object object = objectFactory.getObject();
viewMap.put(name, object);

return object;
}
}

@Override
public Object remove(String name) {
return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
}

@Override
public String getConversationId() {
return null;
}

@Override
public void registerDestructionCallback(String name, Runnable callback) {
// Not supported by JSF for view scope
}

@Override
public Object resolveContextualObject(String key) {
return null;
}
}

并在Spring Boot主类中注册如下:

 @Bean
public static CustomScopeConfigurer customScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.setScopes(Collections.<String, Object>singletonMap(
FacesViewScope.NAME, new FacesViewScope()));
return configurer;
}

当使用 spring 管理我的 View 范围 bean 时,如下所示:

@Component("testBean")
@Scope("view")

页面工作正常,但我收到警告:

c.s.f.application.view.ViewScopeManager  : CDI @ViewScoped bean functionality unavailable

我只在第一次访问该页面时收到此警告,所以我担心此警告是否意味着我做错了什么或可能在将来造成问题。

最佳答案

您只需在您的 Maven 项目的 pom.xml 中添加这些依赖项:

<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>

关于spring - CDI @ViewScoped bean 功能不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41907820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com