Shervin Asgari Shervin Asgari - 1 year ago 133
Java Question

@Autowired works but not @Inject

I have a Resource which injects the following class

public class CustomDozerBeanMapper implements Mapper {
private final DozerBeanMapper beanMapper;

public CustomDozerBeanMapper() {
this.beanMapper = new DozerBeanMapper();
BeanMappingBuilder builder = new BeanMappingBuilder() {
protected void configure() {
//some mapping stuff

public <T> T map(Object o, Class<T> aClass) throws MappingException {
return, aClass);

public void map(Object o, Object o1) throws MappingException {, o1);

public <T> T map(Object o, Class<T> aClass, String s) throws MappingException {
return, aClass, s);

public void map(Object o, Object o1, String s) throws MappingException {, o1, s);

In my applicationContext.xml I have declared

<context:component-scan base-package=""/>
<bean id="customDozerMapper" class="" />

Then in our resource I inject it

class SomeResource {
@Inject CustomDozerMapper customDozerMapper;

//We have loads of other Injects which work just fine, only this class has problems

Caused by: A MultiException has 1 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=CustomDozerBeanMapper,parent=SomeResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1098507248)

at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(
at org.jvnet.hk2.internal.Utilities.justInject(
at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(
at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(
at org.glassfish.jersey.server.spring.SpringComponentProvider$SpringManagedBeanFactory.provide(
at org.jvnet.hk2.internal.FactoryCreator.create(
at org.jvnet.hk2.internal.SystemDescriptor.create(
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(
at org.jvnet.hk2.internal.Utilities.createService(
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(
at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(
at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(

Now if I change and use @Autowired, it works fine
We are using Spring for dependency management, but for some reason h2k is being used, and I get the following exception

Can anyone please explain what the problem might be?

Why does it work with @Autowired and not @Inject

Why is h2k being used, and not Spring?

Answer Source

Probably, the problem may be because of 2 bean declarations (one in the XML configuration and the another one with @Component) and a DI container couldn't able to pick up one of them.

All solutions that are available here:

  1. removing one of the bean definitions (I'd prefer the XML one)
  2. specifying a bean by the @Qualifier or @Named annotation
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download