All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.cdi.CDIAware Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.cdi;

import java.lang.annotation.Annotation;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Qualifier;

/**
 * A base class for Java beans which can not be instantiated via CDI (for
 * example because they are instantiated via reflection by some given framework)
 * but need access to the CDI context.
 * 

* Provides helper methods for looking up beans from CDI or access the CDI * {@link BeanManager}. *

* Alternatively the class {@link CDIObject} can be used which automatically * performs non-constructor injections and post-construct callback based on * {@link Inject} and {@link PostConstruct}. * * @see CDIObject */ public abstract class CDIAware { /** * Provides access to the CDI {@link BeanManager}. * * @return the CDI {@link BeanManager} * @see CDIContext#getBeanManager() */ public BeanManager getBeanManager() { return CDIContext.getBeanManager(); } /** * Looks up a {@link Named} bean within the CDI context. * * @param name * the name of the {@link Named} bean * @param * the type of the bean to lookup * @return the resulting CDI bean * @see CDIContext#lookup(String) */ public E lookup(final String name) { return CDIContext.lookup(name); } /** * Looks up a bean of the given type and with the given {@link Qualifier}s * within the CDI context. * * @param clazz * the {@link Class} of the bean to lookup * @param * the type of the bean to lookup * @param annotations * the {@link Qualifier} annotations of the bean * @return the resulting CDI bean * @see CDIContext#lookup(Class, Annotation...) */ public E lookup(final Class clazz, final Annotation... annotations) { return CDIContext.lookup(clazz, annotations); } /** * Looks up a bean of the given type within the CDI context. * * @param clazz * the {@link Class} of the bean to lookup * @param * the type of the bean to lookup * @return the resulting CDI bean * @see CDIContext#lookup(Class) */ public E lookup(final Class clazz) { return CDIContext.lookup(clazz); } /** * Creates a new {@link InjectionSpec.Builder} for building a CDI lookup via * fluent API. * * @param * the type of the bean to lookup * @return the resulting {@link InjectionSpec.Builder} * @see InjectionSpec#build() */ public InjectionSpec.Builder buildLookup() { return InjectionSpec.build(getBeanManager()); } /** * Performs non-constructor injections and post-construct callbacks on this * bean. * * @see CDIContext#initBean(Object) */ public void initBean() { CDIContext.initBean(this); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy