本文整理汇总了Java中javax.enterprise.inject.spi.BeanManager类的典型用法代码示例。如果您正苦于以下问题:Java BeanManager类的具体用法?Java BeanManager怎么用?Java BeanManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BeanManager类属于javax.enterprise.inject.spi包,在下文中一共展示了BeanManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testProducer
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
@Test
public void testProducer() {
logger.info("starting producer spi test");
CDI<Object> cdi = current();
BeanManager beanManager = cdi.select(BeanManager.class).get();
AnnotatedField<? super AnotherFactory> field = this
.<AnotherFactory, AnnotatedField<AnotherFactory>>getAnnotatedMember(AnotherFactory.class, "jessie",
beanManager);
Bean<AnotherFactory> declaringBean = cast(beanManager.resolve(beanManager.getBeans(AnotherFactory.class)));
ProducerFactory<AnotherFactory> producerFactory = beanManager.getProducerFactory(field, declaringBean);
Producer<Toy> producer = cast(producerFactory.createProducer(null));
assertNotNull(producer);
assertTrue(producer.getInjectionPoints().isEmpty());
Toy jessie = producer.produce(beanManager.<Toy>createCreationalContext(null));
assertEquals("Jessie", jessie.getName());
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:18,代码来源:InjectSPITestCase.java
示例2: testInjectionTarget
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
@Test
public void testInjectionTarget() {
BeanManager beanManager = current().getBeanManager();
// CDI uses an AnnotatedType object to read the annotations of a class
AnnotatedType<String> type = beanManager.createAnnotatedType(String.class);
// The extension uses an InjectionTarget to delegate instantiation,
// dependency injection
// and lifecycle callbacks to the CDI container
InjectionTarget<String> it = beanManager.createInjectionTarget(type);
// each instance needs its own CDI CreationalContext
CreationalContext<String> ctx = beanManager.createCreationalContext(null);
// instantiate the framework component and inject its dependencies
String instance = it.produce(ctx); // call the constructor
it.inject(instance, ctx); // call initializer methods and perform field
// injection
it.postConstruct(instance); // call the @PostConstruct method
// destroy the framework component instance and clean up dependent
// objects
assertNotNull("the String instance is injected now", instance);
assertTrue("the String instance is injected now but it's empty", instance.isEmpty());
it.preDestroy(instance); // call the @PreDestroy method
it.dispose(instance); // it is now safe to discard the instance
ctx.release(); // clean up dependent objects
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:25,代码来源:InjectSPITestCase.java
示例3: getAnnotatedMember
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private <X, A extends AnnotatedMember<? super X>> A getAnnotatedMember(Class<X> javaClass, String memberName,
BeanManager beanManager) {
AnnotatedType<X> type = beanManager.createAnnotatedType(javaClass);
for (AnnotatedField<? super X> field : type.getFields()) {
if (field.getJavaMember().getName().equals(memberName)) {
return (A) field;
}
}
for (AnnotatedMethod<? super X> method : type.getMethods()) {
if (method.getJavaMember().getName().equals(memberName)) {
return (A) method;
}
}
throw new IllegalArgumentException("Member " + memberName + " not found on " + javaClass);
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:17,代码来源:InjectSPITestCase.java
示例4: getInstancesByType
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> List<T> getInstancesByType(Class<T> clazz) {
BeanManager beanManager = getBeanManager();
Type type = clazz;
if (clazz == JsonApiExceptionMapper.class) {
TypeLiteral<JsonApiExceptionMapper<?>> typeLiteral = new TypeLiteral<JsonApiExceptionMapper<?>>() {
};
type = typeLiteral.getType();
}
Set<Bean<?>> beans = beanManager.getBeans(type);
List<T> list = new ArrayList<>();
for (Bean<?> bean : beans) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
T object = (T) beanManager.getReference(bean, type, creationalContext);
list.add(object);
}
return list;
}
示例5: getInstancesByAnnotation
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
@Override
public <A extends Annotation> List<Object> getInstancesByAnnotation(Class<A> annotationClass) {
BeanManager beanManager = getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(Object.class);
List<Object> list = new ArrayList<>();
for (Bean<?> bean : beans) {
Class<?> beanClass = bean.getBeanClass();
Optional<A> annotation = ClassUtils.getAnnotation(beanClass, annotationClass);
if (annotation.isPresent()) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
Object object = beanManager.getReference(bean, beanClass, creationalContext);
list.add(object);
}
}
return list;
}
示例6: registerConfigProducer
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
public void registerConfigProducer(@Observes AfterBeanDiscovery abd, BeanManager bm) {
// excludes type that are already produced by ConfigProducer
Set<Class> types = injectionPoints.stream()
.filter(ip -> ip.getType() instanceof Class
&& ip.getType() != String.class
&& ip.getType() != Boolean.class
&& ip.getType() != Boolean.TYPE
&& ip.getType() != Integer.class
&& ip.getType() != Integer.TYPE
&& ip.getType() != Long.class
&& ip.getType() != Long.TYPE
&& ip.getType() != Float.class
&& ip.getType() != Float.TYPE
&& ip.getType() != Double.class
&& ip.getType() != Double.TYPE
&& ip.getType() != Duration.class
&& ip.getType() != LocalDate.class
&& ip.getType() != LocalTime.class
&& ip.getType() != LocalDateTime.class)
.map(ip -> (Class) ip.getType())
.collect(Collectors.toSet());
types.forEach(type -> abd.addBean(new ConfigInjectionBean(bm, type)));
}
示例7: validate
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
public void validate(@Observes AfterDeploymentValidation add, BeanManager bm) {
List<String> deploymentProblems = new ArrayList<>();
Config config = ConfigProvider.getConfig();
for (InjectionPoint injectionPoint : injectionPoints) {
Type type = injectionPoint.getType();
ConfigProperty configProperty = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class);
if (type instanceof Class) {
String key = getConfigKey(injectionPoint, configProperty);
if (!config.getOptionalValue(key, (Class)type).isPresent()) {
String defaultValue = configProperty.defaultValue();
if (defaultValue == null ||
defaultValue.equals(ConfigProperty.UNCONFIGURED_VALUE)) {
deploymentProblems.add("No Config Value exists for " + key);
}
}
}
}
if (!deploymentProblems.isEmpty()) {
add.addDeploymentProblem(new DeploymentException("Error while validating Configuration\n"
+ String.join("\n", deploymentProblems)));
}
}
示例8: getBeanManager
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
private BeanManager getBeanManager() {
try {
InitialContext ctx = new InitialContext();
return (BeanManager) ctx.lookup("java:comp/BeanManager");
} catch (NamingException e) {
throw new RuntimeException("Unable to lookup CDI bean manager", e);
}
}
示例9: createBeanAdapter
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
/**
*
* @param ip
* @param beanManager
* @return
*/
private <T> Bean<T> createBeanAdapter(InjectionPoint ip, BeanManager beanManager) {
final Type type = ip.getType();
final Class<T> rawType = ReflectionUtils.getRawType(type);
final ContextualLifecycle<T> lifecycleAdapter = new BodyLifecycle<T>(ip, beanManager);
final BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager)
.readFromType(new AnnotatedTypeBuilder<T>().readFromType(rawType).create())
.beanClass(Body.class) // see https://issues.jboss.org/browse/WELD-2165
.name(ip.getMember().getName())
.qualifiers(ip.getQualifiers())
.beanLifecycle(lifecycleAdapter)
.scope(Dependent.class)
.passivationCapable(false)
.alternative(false)
.nullable(true)
.id("BodyBean#" + type.toString())
.addType(type); //java.lang.Object needs to be present (as type) in any case
return beanBuilder.create();
}
示例10: registerNoSQLSourceBeans
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
/**
*/
void registerNoSQLSourceBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
if (bm.getBeans(clusterClass, DefaultLiteral.INSTANCE).isEmpty()) {
// Iterate profiles and create Cluster/Session bean for each profile, that application code can @Inject
for(String profile: getService().profileNames()) {
log.log(Level.INFO, "Registering bean for profile {0}", profile);
abd.addBean(bm.createBean(
new ClusterBeanAttributes(bm.createBeanAttributes(bm.createAnnotatedType(clusterClass)), profile),
clusterClass, new ClusterProducerFactory(profile, clusterClass)));
abd.addBean(bm.createBean(
new SessionBeanAttributes(bm.createBeanAttributes(bm.createAnnotatedType(sessionClass)), profile),
sessionClass, new SessionProducerFactory(profile, sessionClass)));
}
} else {
log.log(Level.INFO, "Application contains a default Cluster Bean, automatic registration will be disabled");
}
}
示例11: registerNoSQLSourceBeans
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
void registerNoSQLSourceBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
if (bm.getBeans(mongoClientClass, DefaultLiteral.INSTANCE).isEmpty()) {
// Iterate profiles and create Cluster/Session bean for each profile, that application code can @Inject
for(String profile: getService().profileNames()) {
log.log(Level.INFO, "Registering bean for profile {0}", profile);
abd.addBean(bm.createBean(
new MongoClientBeanAttributes(bm.createBeanAttributes(bm.createAnnotatedType(mongoClientClass)), profile),
mongoClientClass, new MongoClientProducerFactory(profile, mongoClientClass)));
abd.addBean(bm.createBean(
new MongoDatabaseBeanAttributes(bm.createBeanAttributes(bm.createAnnotatedType(mongoDatabaseClass)), profile),
mongoDatabaseClass, new MongoDatabaseProducerFactory(profile, mongoDatabaseClass)));
}
} else {
log.log(Level.INFO, "Application contains a default MongoClient Bean, automatic registration will be disabled");
}
}
示例12: getBean
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
BeanManager bm = getBeanManager();
Set<Bean<?>> beans = bm.getBeans(clazz);
if (beans == null || beans.isEmpty()) {
return null;
}
Bean<T> bean = (Bean<T>) beans.iterator().next();
CreationalContext<T> ctx = bm.createCreationalContext(bean);
T o = (T) bm.getReference(bean, clazz, ctx);
return o;
}
示例13: addRouteToContext
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
private boolean addRouteToContext(Bean<?> routeBean, Bean<?> contextBean, BeanManager manager, AfterDeploymentValidation adv) {
try {
CamelContext context = getReference(manager, CamelContext.class, contextBean);
try {
Object route = getReference(manager, Object.class, routeBean);
if (route instanceof RoutesBuilder) {
context.addRoutes((RoutesBuilder) route);
} else if (route instanceof RouteContainer) {
context.addRouteDefinitions(((RouteContainer) route).getRoutes());
} else {
throw new IllegalArgumentException(
"Invalid routes type [" + routeBean.getBeanClass().getName() + "], "
+ "must be either of type RoutesBuilder or RouteContainer!");
}
return true;
} catch (Exception cause) {
adv.addDeploymentProblem(
new InjectionException(
"Error adding routes of type [" + routeBean.getBeanClass().getName() + "] "
+ "to Camel context [" + context.getName() + "]", cause));
}
} catch (Exception exception) {
adv.addDeploymentProblem(exception);
}
return false;
}
示例14: afterBeanDiscovery
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
final CommandContextImpl commandContext = new CommandContextImpl();
// Register the command context
event.addContext(commandContext);
// Register the command context bean using CDI 2 configurators API
event.addBean()
.addType(CommandContext.class)
.createWith(ctx -> new InjectableCommandContext(commandContext, beanManager))
.addQualifier(Default.Literal.INSTANCE)
.scope(Dependent.class)
.beanClass(CommandExtension.class);
// Register the CommandExecution bean using CDI 2 configurators API
event.addBean()
.createWith(ctx -> commandContext.getCurrentCommandExecution())
.addType(CommandExecution.class)
.addQualifier(Default.Literal.INSTANCE)
.scope(CommandScoped.class)
.beanClass(CommandExtension.class);
}
示例15: afterProcessViewEvent
import javax.enterprise.inject.spi.BeanManager; //导入依赖的package包/类
/**
* Perform the work we need to do at AfterProcessViewEvent time.
*
* @param event the event.
*/
public void afterProcessViewEvent(@Observes AfterProcessViewEvent event) {
if (request.getAttribute(SCOPE_ID) != null) {
String scopeId = (String) request.getAttribute(SCOPE_ID);
HttpSession session = request.getSession();
final String sessionKey = SCOPE_ID + "-" + scopeId;
Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
if (null != scopeMap) {
scopeMap.entrySet().stream().forEach((entrySet) -> {
String key = entrySet.getKey();
Object value = entrySet.getValue();
if (key.startsWith(INSTANCE)) {
BeanManager beanManager = CdiUtils.getApplicationBeanManager();
Bean<?> bean = beanManager.resolve(beanManager.getBeans(value.getClass()));
destroy(bean);
}
});
scopeMap.clear();
session.removeAttribute(sessionKey);
}
}
}