BeanFactoryUtils.beanOfType vs BeanFactoryUtils.beansOfTypeIncludingAncestors in spring

The presence of this class:

public interface DeviceRule {
}

      

and this one

@Service(value = "androidRule")
@Transactional(propagation = Propagation.REQUIRED)
public class AndroidRule implements DeviceRule {
..
}

      

this method works fine, called in the service getting AndroidRule

Map<?, DeviceRule> map = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, DeviceRule.class, true, false);

      

but not this one:

BeanFactoryUtils.beanOfType(context, AndroidRule.class, true, false);

      

since i got this error:

 org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'androidRule' must be of type [com.devices.AndroidRule], but was actually of type [com.sun.proxy.$Proxy302]:org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean named 'androidRule' must be of type [com.devices.AndroidRule], but was actually of type [com.sun.proxy.$Proxy302]

      

any idea?

+3


source to share


1 answer


When you add annotation @Transactional

to a class, spring creates a proxy for this to improve it with start, commit and rollback.

When you call BeanFactoryUtils.beanOfType

it tries to return a unique bean of the given type. And in your case, there are two (including proxies). Therefore he fails.



You can check the amount of beans, BeanFactoryUtils.beansOfTypeIncludingAncestors

.

+2


source







All Articles