org.apache.struts2.cdi

## Class CdiObjectFactory

- java.lang.Object
- - com.opensymphony.xwork2.ObjectFactory
  - - org.apache.struts2.cdi.CdiObjectFactory

- All Implemented Interfaces:
    Serializable

      public class CdiObjectFactory
      extends ObjectFactory

  CdiObjectFactory allows Struts 2 managed objects, like Actions, Interceptors or Results, to be injected by a Contexts and Dependency Injection container (JSR299 / WebBeans). The BeanManager instance will be searched in the container's JNDI context, according to following algorithm:
  - if a value for configuration constant `struts.objectFactory.cdi.jndiKey` is given, this key will be looked up
  - if no BeanManager found so far, look under `CDI_JNDIKEY_BEANMANAGER_COMP`
  - if no BeanManager found so far, look under `CDI_JNDIKEY_BEANMANAGER_APP`
  - if no BeanManager found so far, look under `CDI_JNDIKEY_BEANMANAGER_COMP_ENV`
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected javax.enterprise.inject.spi.BeanManager` | `beanManager`
    `static String` | `CDI_JNDIKEY_BEANMANAGER_APP` The key under which the BeanManager can be found according to JBoss Weld docs
    `static String` | `CDI_JNDIKEY_BEANMANAGER_COMP` The key under which the BeanManager can be found according to CDI API docs
    `static String` | `CDI_JNDIKEY_BEANMANAGER_COMP_ENV` The key under which the BeanManager can be found in pure Servlet containers according to JBoss Weld docs.
    `static String` | `STRUTS_OBJECT_FACTORY_CDI_JNDI_KEY`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CdiObjectFactory()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Object` | `buildBean(String className, Map<String,Object> extraContext, boolean injectInternal)`
    `protected javax.enterprise.context.spi.CreationalContext` | `buildNonContextualCreationalContext(javax.enterprise.inject.spi.BeanManager beanManager)` Simple wrapper for CreationalContext creation.
    `protected javax.enterprise.inject.spi.BeanManager` | `findBeanManager()` Try to find the CDI BeanManager from JNDI context.
    `protected javax.enterprise.inject.spi.InjectionTarget<?>` | `getInjectionTarget(Class<?> clazz)` Get a InjectionTarget instance for a given class.
    `boolean` | `isNoArgConstructorRequired()` Allow constructor injection
    `protected javax.enterprise.inject.spi.BeanManager` | `lookup(Context context, String jndiKeyToCheck)` Lookup the given JNDI key in the given context.
    `void` | `setJndiKey(String jndiKey)`

    - ### Methods inherited from class com.opensymphony.xwork2.ObjectFactory

      `buildAction, buildBean, buildBean, buildConverter, buildInterceptor, buildResult, buildUnknownHandler, buildValidator, getClassInstance, injectInternalBeans, setActionFactory, setClassLoader, setContainer, setConverterFactory, setInterceptorFactory, setResultFactory, setUnknownHandlerFactory, setValidatorFactory`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### CDI_JNDIKEY_BEANMANAGER_COMP

          public static final String CDI_JNDIKEY_BEANMANAGER_COMP

      The key under which the BeanManager can be found according to CDI API docs
      See Also:
        Constant Field Values

    - #### CDI_JNDIKEY_BEANMANAGER_APP

          public static final String CDI_JNDIKEY_BEANMANAGER_APP

      The key under which the BeanManager can be found according to JBoss Weld docs
      See Also:
        Constant Field Values

    - #### CDI_JNDIKEY_BEANMANAGER_COMP_ENV

          public static final String CDI_JNDIKEY_BEANMANAGER_COMP_ENV

      The key under which the BeanManager can be found in pure Servlet containers according to JBoss Weld docs.
      See Also:
        Constant Field Values

    - #### STRUTS_OBJECT_FACTORY_CDI_JNDI_KEY

          public static final String STRUTS_OBJECT_FACTORY_CDI_JNDI_KEY

      See Also:
        Constant Field Values

    - #### beanManager

          protected javax.enterprise.inject.spi.BeanManager beanManager

  - ### Constructor Detail

    - #### CdiObjectFactory

          public CdiObjectFactory()

  - ### Method Detail

    - #### setJndiKey

          public void setJndiKey(String jndiKey)

    - #### findBeanManager

          protected javax.enterprise.inject.spi.BeanManager findBeanManager()

      Try to find the CDI BeanManager from JNDI context. First, if provided, the key given by struts.objectFactory.cdi.jndiKey will be checked. Then, if nothing was found or no explicit configuration was given, the key `CDI_JNDIKEY_BEANMANAGER_COMP` will be tested. If nothing is found there, the key `CDI_JNDIKEY_BEANMANAGER_APP` will be checked. If still nothing is found there, the key `CDI_JNDIKEY_BEANMANAGER_COMP_ENV` will be checked.
      Returns:
        the BeanManager, if found. `null` otherwise.

    - #### lookup

          protected javax.enterprise.inject.spi.BeanManager lookup(Context context,
                                                                   String jndiKeyToCheck)

      Lookup the given JNDI key in the given context.
      Parameters:
        `context` - the context to use for lookup.
        `jndiKeyToCheck` - the key to lookup.
      Returns:
        the BeanManager, if found; `null` if not found or `NamingException` was thrown.

    - #### buildBean

          public Object buildBean(String className,
                                  Map<String,Object> extraContext,
                                  boolean injectInternal)
                           throws Exception

      Overrides:
        `buildBean` in class `ObjectFactory`
      Throws:
        `Exception`

    - #### getInjectionTarget

          protected javax.enterprise.inject.spi.InjectionTarget<?> getInjectionTarget(Class<?> clazz)

      Get a InjectionTarget instance for a given class. If the appropriate target is not found in cache, a nw instance will be created.
      Parameters:
        `clazz` - The class to get a InjectionTarget instance for.
      Returns:
        if found in cache, an existing instance. A new instance otherwise.

    - #### buildNonContextualCreationalContext

          protected javax.enterprise.context.spi.CreationalContext buildNonContextualCreationalContext(javax.enterprise.inject.spi.BeanManager beanManager)

      Simple wrapper for CreationalContext creation.
      Parameters:
        `beanManager` - the BeanManager to use for creating the context.
      Returns:
        the context to use, if given BeanManager was not `null`. `null` otherwise.

    - #### isNoArgConstructorRequired

          public boolean isNoArgConstructorRequired()

      Allow constructor injection
      Overrides:
        `isNoArgConstructorRequired` in class `ObjectFactory`

Copyright © 2000–2025 Apache Software Foundation. All rights reserved.