Search code examples
springdependency-injectioncdi

@Autowired works but not @Inject


I have a Resource which injects the following class

@Component
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
            }
        };
        beanMapper.addMapping(builder);
    }

    @Override
    public <T> T map(Object o, Class<T> aClass) throws MappingException {
        return beanMapper.map(o, aClass);
    }

    @Override
    public void map(Object o, Object o1) throws MappingException {
        beanMapper.map(o, o1);
    }

    @Override
    public <T> T map(Object o, Class<T> aClass, String s) throws MappingException {
        return beanMapper.map(o, aClass, s);
    }

    @Override
    public void map(Object o, Object o1, String s) throws MappingException {
        beanMapper.map(o, o1, s);
    }
}

In my applicationContext.xml I have declared

<context:annotation-config/>
<context:component-scan base-package="foo.bar"/>
<bean id="customDozerMapper" class="foo.bar.CustomDozerBeanMapper" />

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(ThreeThirtyResolver.java:75)
    at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:947)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:975)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:965)
    at org.glassfish.jersey.server.spring.SpringComponentProvider$SpringManagedBeanFactory.provide(SpringComponentProvider.java:191)
    at org.jvnet.hk2.internal.FactoryCreator.create(FactoryCreator.java:153)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2072)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:761)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:700)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)

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?


Solution

  • 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