Annotation Type New


  • @Target({FIELD,PARAMETER,METHOD,TYPE})
    @Retention(RUNTIME)
    @Documented
    @Qualifier
    public @interface New
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.Class<?> value
      May be used to declare a class which should be used for injection.
    • Element Detail

      • value

        java.lang.Class<?> value

        May be used to declare a class which should be used for injection. This defaults to the type which is defined at the injection point.

        Technically this is a qualifier, but it has a very special handling defined by the specification. It will create a new Contextual Instance of the given class by calling the default constructor. The created Contextual Instance will be treated as being @Dependent to the instance the injection point belongs to.

        @New also works for creating Contextual Instances of classes which are not part of a bean archive (BDA, aka a jar with a META-INF/beans.xml). Note that from a practical point @New is rarely useful. If you don't have a beans.xml then you will most probably also not have any CDI feature in that class. and if you otoh do have such a BDA, then you can inject the bean directly anyway. The only real usage is to inject a new 'dependent' instance of a CDI bean which has a different scope already defined.

        Example:

         @Inject @New SomeClass instance;
         

        Attention: @New only works for InjectionPoints, it is not possible to resolve a new-bean programatically via BeanManager.getBeans(java.lang.reflect.Type, java.lang.annotation.Annotation...) if there was no @New InjectionPoint of that type in the scanned classes.

        Returns:
        the class of the bean which should be injected
        Default:
        javax.enterprise.inject.New.class