[JBoss JIRA] Commented: (CDI-14) Add instance() method to BeanManager
by Arne Limburg (JIRA)
[ https://issues.jboss.org/browse/CDI-14?page=com.atlassian.jira.plugin.sys... ]
Arne Limburg commented on CDI-14:
---------------------------------
We even could go a step further and provide an interface to the application developer without the need of a bean-manager.
What about adding the following class to the spec:
{noformat}
public class ObjectInstance implements Instance<Object> {
private Instance<Object> delegate;
public ObjectInstance() {
//lookup bean-manager via JNDI,
//get Instance<Object> from bean-manager
//and set delegate
}
//methods are delegated to the delegate
}
{noformat}
Every CDI-user could instantiate this class and use it to get contextual instances.
> Add instance() method to BeanManager
> ------------------------------------
>
> Key: CDI-14
> URL: https://issues.jboss.org/browse/CDI-14
> Project: CDI Specification Issues
> Issue Type: Feature Request
> Components: Resolution
> Affects Versions: 1.0
> Reporter: Pete Muir
> Fix For: 1.1 (Proposed)
>
>
> Currently obtaining a contextual reference is quite a complex operation, adding a method like:
> Instance<Object> instance();
> would make it much easier.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 8 months
general goal for CDI-1.1
by Mark Struberg
Hi folks!
I've seen lots of different ideas in the CDI issue tracker.
95% of those ideas are really great.
Some of them are easy little corrections/clarifications, others are pretty huge changes/extensions to the fundamental CDI mechanics.
So please let me ask you what the goal of CDI-1.1 is?
Are we preparing for a quick MR or is it a rather a full scale next version? And what's the proposed time scale?
LieGrue,
strub
14 years, 8 months
[JBoss JIRA] Commented: (CDI-14) Add instance() method to BeanManager
by Pete Muir (JIRA)
[ https://issues.jboss.org/browse/CDI-14?page=com.atlassian.jira.plugin.sys... ]
Pete Muir commented on CDI-14:
------------------------------
Rick, I think this points more to a communication issue than an API failing. BeanManager is intended as a low level API (in fact it is even labeled an SPI). Instance<> is intended as a user application facing API (so not low level at all). It's far more user friendly than BeanManager (it *just* focuses on the case of programmatic instantiation, none of the other stuff that is on BeanManager), and offers exactly the methods you want. In fact Lincoln's utility would
a) introduce a smaller learning curve
b) be more powerful
if rather than inventing a new API for this, simply allowed you to look grab an Instance<> (to be fair it took me well over a year to even realise this problem was trivial to address as well ;-).
So in response to your "Spring app context" point, what I would say is that the issue isn't that BM doesn't have the right methods, it's just that
a) you were looking in the wrong place
b) the spec did a very very bad job of showing you the right place to look (actually it showed you the wrong place!)
Let's fix this.
Rick, open an issue for allowing Instance to do a name based selection and give your use cases there.
> Add instance() method to BeanManager
> ------------------------------------
>
> Key: CDI-14
> URL: https://issues.jboss.org/browse/CDI-14
> Project: CDI Specification Issues
> Issue Type: Feature Request
> Components: Resolution
> Affects Versions: 1.0
> Reporter: Pete Muir
> Fix For: 1.1 (Proposed)
>
>
> Currently obtaining a contextual reference is quite a complex operation, adding a method like:
> Instance<Object> instance();
> would make it much easier.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 8 months
[JBoss JIRA] Commented: (CDI-14) Add instance() method to BeanManager
by Richard Hightower (JIRA)
[ https://issues.jboss.org/browse/CDI-14?page=com.atlassian.jira.plugin.sys... ]
Richard Hightower commented on CDI-14:
--------------------------------------
Peter et al, good points. So Instance is the way to do this, and if you expose and instance, you can use instance. Got it.
Lincoln, We wrote a utility class as well and we use that a lot. I don't think we are normal users (in every sense of the word) :).
I just think adding a couple of extra methods that hide the details of Instance is a good idea for an easier to use API.
I am guessing that not everyone wants to be an expert on CDI, but they may want to grab a bean.
I know I was quite surprised the amount of learning curve it took me to just look a bean up by name. I compare this to the Spring applicationContext, which does have these types of methods. I just think that BeanManager should have some easy to use facade methods to do common things.
There are a lot of people who don't care so much about instance and low level details of CDI. They just want a darn bean. :)
> Add instance() method to BeanManager
> ------------------------------------
>
> Key: CDI-14
> URL: https://issues.jboss.org/browse/CDI-14
> Project: CDI Specification Issues
> Issue Type: Feature Request
> Components: Resolution
> Affects Versions: 1.0
> Reporter: Pete Muir
> Fix For: 1.1 (Proposed)
>
>
> Currently obtaining a contextual reference is quite a complex operation, adding a method like:
> Instance<Object> instance();
> would make it much easier.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 8 months
[JBoss JIRA] Commented: (CDI-14) Add instance() method to BeanManager
by Pete Muir (JIRA)
[ https://issues.jboss.org/browse/CDI-14?page=com.atlassian.jira.plugin.sys... ]
Pete Muir commented on CDI-14:
------------------------------
The original proposal is to add a utility method to BeanManager which returns an Instance<Object> which can be used to do programmatic lookup. Apart from the isDependentScoped method Instance exposes all of the methods you have above, and also allows for subselection. We can add a select(String beanName) to Instance easily if needed (Rick indicates that it is). This prevents us duplicating an existing API and means we don't have to make BM much more complex.
> Add instance() method to BeanManager
> ------------------------------------
>
> Key: CDI-14
> URL: https://issues.jboss.org/browse/CDI-14
> Project: CDI Specification Issues
> Issue Type: Feature Request
> Components: Resolution
> Affects Versions: 1.0
> Reporter: Pete Muir
> Fix For: 1.1 (Proposed)
>
>
> Currently obtaining a contextual reference is quite a complex operation, adding a method like:
> Instance<Object> instance();
> would make it much easier.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 8 months
[JBoss JIRA] Commented: (CDI-14) Add instance() method to BeanManager
by Lincoln Baxter III (JIRA)
[ https://issues.jboss.org/browse/CDI-14?page=com.atlassian.jira.plugin.sys... ]
Lincoln Baxter III commented on CDI-14:
---------------------------------------
{code}@Inject private Instance<Object> obj;{code} only works when injection is already available; I believe they are asking for methods on the beanManager to reduce things like the BeanManagerUtils that I wrote, which even on the Seam Team, we use everywhere to get instances of contextual objects.
{code}package org.jboss.seam.faces.util;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.inject.Inject;
import org.jboss.seam.faces.component.FormValidationTypeOverrideExtension;
/**
* A utility providing common functions to simply use of {@link BeanManager}
*
* @author <a href="mailto:lincolnbaxter@gmail.com>Lincoln Baxter, III</a>
*/
public class BeanManagerUtils {
@Inject
private BeanManager manager;
@Inject
private FormValidationTypeOverrideExtension classExtension;
/**
* Perform @{@link Inject} on an object as if it were a bean managed by CDI.
*
* @param instance
*/
@SuppressWarnings("unchecked")
public void injectNonContextualInstance(final Object instance) {
if (instance != null) {
CreationalContext<Object> creationalContext = manager.createCreationalContext(null);
InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) manager
.createInjectionTarget(getAnnotatedType(instance));
injectionTarget.inject(instance, creationalContext);
}
}
private AnnotatedType<? extends Object> getAnnotatedType(final Object instance) {
AnnotatedType<?> result = null;
if (classExtension.hasOverriddenType(instance.getClass())) {
result = classExtension.getOverriddenType(instance.getClass());
} else {
result = manager.createAnnotatedType(instance.getClass());
}
return result;
}
/**
* Determine if a bean is {@link Dependent} scoped.
*/
@SuppressWarnings("unchecked")
public <T> boolean isDependentScoped(final Class<T> type) {
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
if (bean != null) {
return Dependent.class.equals(bean.getScope());
}
return false;
}
/**
* Get a single CDI managed instance of a specific class. Return only the first result if multiple beans are available.
*
* @param type The class for which to return an instance.
* @return The managed instance, or null if none could be provided.
*/
public <T> T getContextualInstance(final Class<T> type) {
return getContextualInstance(manager, type);
}
/**
* Get a single CDI managed instance of a specific class. Return only the first result if multiple beans are available.
* <p/>
* <b>NOTE:</b> Using this method should be avoided at all costs.
*
* @param manager The bean manager with which to perform the lookup.
* @param type The class for which to return an instance.
* @return The managed instance, or null if none could be provided.
*/
@SuppressWarnings("unchecked")
public static <T> T getContextualInstance(final BeanManager manager, final Class<T> type) {
T result = null;
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
if (bean != null) {
CreationalContext<T> context = manager.createCreationalContext(bean);
if (context != null) {
result = (T) manager.getReference(bean, type, context);
}
}
return result;
}
/**
* Get all CDI managed instances of a specific class. Return results in a {@link List} in no specific order.
*
* @param type The class for which to return instances.
*/
@SuppressWarnings("unchecked")
public <T> List<T> getContextualInstances(final Class<T> type) {
List<T> result = new ArrayList<T>();
for (Bean<?> bean : manager.getBeans(type)) {
CreationalContext<T> context = (CreationalContext<T>) manager.createCreationalContext(bean);
if (context != null) {
result.add((T) manager.getReference(bean, type, context));
}
}
return result;
}
}{code}
> Add instance() method to BeanManager
> ------------------------------------
>
> Key: CDI-14
> URL: https://issues.jboss.org/browse/CDI-14
> Project: CDI Specification Issues
> Issue Type: Feature Request
> Components: Resolution
> Affects Versions: 1.0
> Reporter: Pete Muir
> Fix For: 1.1 (Proposed)
>
>
> Currently obtaining a contextual reference is quite a complex operation, adding a method like:
> Instance<Object> instance();
> would make it much easier.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 8 months
[JBoss JIRA] Issue Comment Edited: (CDI-14) Add instance() method to BeanManager
by Lincoln Baxter III (JIRA)
[ https://issues.jboss.org/browse/CDI-14?page=com.atlassian.jira.plugin.sys... ]
Lincoln Baxter III edited comment on CDI-14 at 5/11/11 9:52 AM:
----------------------------------------------------------------
{code}@Inject private Instance<Object> obj;{code} only works when injection is already available; I believe they are asking for methods on the beanManager to reduce things like the BeanManagerUtils that I wrote, which even on the Seam Team, we use everywhere to get instances of contextual objects.
{code}package org.jboss.seam.faces.util;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.inject.Inject;
/**
* A utility providing common functions to simply use of {@link BeanManager}
*
* @author <a href="mailto:lincolnbaxter@gmail.com>Lincoln Baxter, III</a>
*/
public class BeanManagerUtils {
@Inject
private BeanManager manager;
/**
* Determine if a bean is {@link Dependent} scoped.
*/
@SuppressWarnings("unchecked")
public <T> boolean isDependentScoped(final Class<T> type) {
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
if (bean != null) {
return Dependent.class.equals(bean.getScope());
}
return false;
}
/**
* Get a single CDI managed instance of a specific class. Return only the first result if multiple beans are available.
*
* @param type The class for which to return an instance.
* @return The managed instance, or null if none could be provided.
*/
public <T> T getContextualInstance(final Class<T> type) {
return getContextualInstance(manager, type);
}
/**
* Get a single CDI managed instance of a specific class. Return only the first result if multiple beans are available.
* <p/>
* <b>NOTE:</b> Using this method should be avoided if container provided injection is available.
*
* @param manager The bean manager with which to perform the lookup.
* @param type The class for which to return an instance.
* @return The managed instance, or null if none could be provided.
*/
@SuppressWarnings("unchecked")
public static <T> T getContextualInstance(final BeanManager manager, final Class<T> type) {
T result = null;
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
if (bean != null) {
CreationalContext<T> context = manager.createCreationalContext(bean);
if (context != null) {
result = (T) manager.getReference(bean, type, context);
}
}
return result;
}
/**
* Get all CDI managed instances of a specific class. Return results in a {@link List} in no specific order.
*
* @param type The class for which to return instances.
*/
@SuppressWarnings("unchecked")
public <T> List<T> getContextualInstances(final Class<T> type) {
List<T> result = new ArrayList<T>();
for (Bean<?> bean : manager.getBeans(type)) {
CreationalContext<T> context = (CreationalContext<T>) manager.createCreationalContext(bean);
if (context != null) {
result.add((T) manager.getReference(bean, type, context));
}
}
return result;
}
}{code}
was (Author: lincolnthree):
{code}@Inject private Instance<Object> obj;{code} only works when injection is already available; I believe they are asking for methods on the beanManager to reduce things like the BeanManagerUtils that I wrote, which even on the Seam Team, we use everywhere to get instances of contextual objects.
{code}package org.jboss.seam.faces.util;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.inject.Inject;
import org.jboss.seam.faces.component.FormValidationTypeOverrideExtension;
/**
* A utility providing common functions to simply use of {@link BeanManager}
*
* @author <a href="mailto:lincolnbaxter@gmail.com>Lincoln Baxter, III</a>
*/
public class BeanManagerUtils {
@Inject
private BeanManager manager;
@Inject
private FormValidationTypeOverrideExtension classExtension;
/**
* Perform @{@link Inject} on an object as if it were a bean managed by CDI.
*
* @param instance
*/
@SuppressWarnings("unchecked")
public void injectNonContextualInstance(final Object instance) {
if (instance != null) {
CreationalContext<Object> creationalContext = manager.createCreationalContext(null);
InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) manager
.createInjectionTarget(getAnnotatedType(instance));
injectionTarget.inject(instance, creationalContext);
}
}
private AnnotatedType<? extends Object> getAnnotatedType(final Object instance) {
AnnotatedType<?> result = null;
if (classExtension.hasOverriddenType(instance.getClass())) {
result = classExtension.getOverriddenType(instance.getClass());
} else {
result = manager.createAnnotatedType(instance.getClass());
}
return result;
}
/**
* Determine if a bean is {@link Dependent} scoped.
*/
@SuppressWarnings("unchecked")
public <T> boolean isDependentScoped(final Class<T> type) {
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
if (bean != null) {
return Dependent.class.equals(bean.getScope());
}
return false;
}
/**
* Get a single CDI managed instance of a specific class. Return only the first result if multiple beans are available.
*
* @param type The class for which to return an instance.
* @return The managed instance, or null if none could be provided.
*/
public <T> T getContextualInstance(final Class<T> type) {
return getContextualInstance(manager, type);
}
/**
* Get a single CDI managed instance of a specific class. Return only the first result if multiple beans are available.
* <p/>
* <b>NOTE:</b> Using this method should be avoided at all costs.
*
* @param manager The bean manager with which to perform the lookup.
* @param type The class for which to return an instance.
* @return The managed instance, or null if none could be provided.
*/
@SuppressWarnings("unchecked")
public static <T> T getContextualInstance(final BeanManager manager, final Class<T> type) {
T result = null;
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
if (bean != null) {
CreationalContext<T> context = manager.createCreationalContext(bean);
if (context != null) {
result = (T) manager.getReference(bean, type, context);
}
}
return result;
}
/**
* Get all CDI managed instances of a specific class. Return results in a {@link List} in no specific order.
*
* @param type The class for which to return instances.
*/
@SuppressWarnings("unchecked")
public <T> List<T> getContextualInstances(final Class<T> type) {
List<T> result = new ArrayList<T>();
for (Bean<?> bean : manager.getBeans(type)) {
CreationalContext<T> context = (CreationalContext<T>) manager.createCreationalContext(bean);
if (context != null) {
result.add((T) manager.getReference(bean, type, context));
}
}
return result;
}
}{code}
> Add instance() method to BeanManager
> ------------------------------------
>
> Key: CDI-14
> URL: https://issues.jboss.org/browse/CDI-14
> Project: CDI Specification Issues
> Issue Type: Feature Request
> Components: Resolution
> Affects Versions: 1.0
> Reporter: Pete Muir
> Fix For: 1.1 (Proposed)
>
>
> Currently obtaining a contextual reference is quite a complex operation, adding a method like:
> Instance<Object> instance();
> would make it much easier.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 8 months