CDI @ViewScoped bean 功能不可用

Mah*_*leh 2 jsf spring jsf-2.2 spring-boot

我在 Spring Boot 1.4.4 中使用 JSF 2.2.14,我定义了一个自定义视图范围,如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并将其注册到 Spring Boot 主类中,如下所示:

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

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

@Component("testBean")
@Scope("view")
Run Code Online (Sandbox Code Playgroud)

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

c.s.f.application.view.ViewScopeManager  : CDI @ViewScoped bean functionality unavailable
Run Code Online (Sandbox Code Playgroud)

我仅在第一次访问该页面时收到此警告,因此我担心此警告是否意味着我做错了什么或将来可能会导致问题。

小智 6

你只需要在你的 maven 项目的 pom.xml 上添加这些依赖:

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)