gpt4 book ai didi

spring - @Autowired 有效但 @Inject 无效

转载 作者:行者123 更新时间:2023-12-02 03:10:14 24 4
gpt4 key购买 nike

我有一个注入(inject)以下类的资源

@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);
}
}

在我的 applicationContext.xml 中声明

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

然后在我们的资源中我注入(inject)它

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)

现在,如果我更改并使用@Autowired,它就可以正常工作我们正在使用 Spring 进行依赖管理,但由于某种原因正在使用 h2k,我得到以下异常

谁能解释一下可能是什么问题?

为什么它使用@Autowired 而不是@Inject

为什么使用 h2k 而不是 Spring?

最佳答案

问题可能是因为 2 个 bean 声明(一个在 XML 配置中,另一个带有 @Component),DI 容器无法获取其中一个。

此处提供的所有解决方案:

  1. 删除其中一个 bean 定义(我更喜欢 XML 定义)
  2. 通过@Qualifier@Named 注解指定一个bean

关于spring - @Autowired 有效但 @Inject 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com