CDI @ViewScoped bean功能不可用

时间:2017-01-28 08:34:49

标签: spring jsf spring-boot jsf-2.2

我正在使用 JSF 2.2.14 并且 springboot 1.4.4 我已经定义了一个自定义视图范围如下:

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;
    }
}

并将其注册在springboot主类中,如下所示:

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

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

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

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

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

我第一次访问该页面时才收到此警告,所以我担心如果这个警告意味着我做错了或将来可能会出现问题,请告知。

1 个答案:

答案 0 :(得分:4)

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

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