Search code examples
debuggingjsffaceletscdimanaged-bean

JSF <ui:debug> not showing CDI beans


When I define my managed bean as a CDI bean (@Named) it is not shown by the ui:debug popup. If I change the definition to a JSF @ManagedBean it shows up in the scoped variables just fine. Is there anything extra I need to do to make this work? I am using Mojarra 2.1.


Solution

  • CDI managed beans are not stored as direct attributes of the request/session/application scope. They're abstracted away behind the CDI context, for which it's in turn implementation dependent (e.g. Weld vs OpenWebBeans vs others) how exactly they're referenced in the scope. The <ui:debug> doesn't offer any builtin facilities to display active CDI managed beans (yet?).

    Your best bet is obtaining them manually. You can use the following utility method for that (which will be available in upcoming OmniFaces 1.7's Beans utility class):

    public static Map<Object, String> getActiveReferences(BeanManager beanManager, Class<? extends Annotation> scope) {
        Map<Object, String> activeReferences = new HashMap<Object, String>();
        Set<Bean<?>> beans = beanManager.getBeans(Object.class);
        Context context = beanManager.getContext(scope);
    
        for (Bean<?> bean : beans) {
            Object reference = context.get(bean);
    
            if (reference != null) {
                activeReferences.put(reference, bean.getName());
            }
        }
    
        return Collections.unmodifiableMap(activeReferences);
    }
    

    Here's how you can use it:

    @Inject
    private BeanManager manager;
    
    public void collect() {
        Map<Object, String> requestScopedBeans = Beans.getActiveReferences(manager, RequestScoped.class);
        // Map key represents the instance and map value represents the managed bean name, if any.
    
        // ...
    } 
    

    Keep in mind that this is a relatively expensive job. So really use it for debugging only.