Java Spring bean 不使用 @Component 注释自动装配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23623398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring bean not autowiring with @Component annotation
提问by IcedDante
Not sure what I'm doing wrong, but I have a webapp configuration that works in Unit Testing but dies in production for a Component bean.
不知道我做错了什么,但我有一个 webapp 配置,它在单元测试中工作,但在组件 bean 的生产中死亡。
The bean extends WebServiceGatewaySupport
and extends an Interface.
bean 扩展WebServiceGatewaySupport
并扩展了一个接口。
I define it in java thusly:
我这样在java中定义它:
@Component("myShippingImplementation")
public class MyShippingImplemenation extends WebServiceGatewaySupport implements ShippingImplementation {
private String addressValidationUri;
public String getAddressValidationUri() {
return addressValidationUri;
}
public void setAddressValidationUri(String addressValidationUri) {
this.addressValidationUri = addressValidationUri;
}
}
and the XML bean configuration is:
并且 XML bean 配置是:
<bean id="myShippingImplementation" class="com.cerp.service.shipping.MyShippingImplemenation" autowire="byType">
<property name="addressValidationUri" value="https://www.testurl.com" />
<property name="defaultUri" value="https://www.alturl.com" />
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
</bean>
If I wire this in a Unit test it works:
如果我在单元测试中连接它,它会起作用:
public class MyServiceClientTest extends BaseWebServiceTest {
@Autowired MyShippingImplemenation c;
(BaseWebServiceTest uses SpringJUnit4ClassRunner)
(BaseWebServiceTest 使用 SpringJUnit4ClassRunner)
but if I wire it in a Controller and run it off Tomcat with the following:
但是如果我将它连接到控制器中并使用以下命令从 Tomcat 运行它:
@Controller
@RequestMapping(value="/work/settings")
public class SettingsController {
@Autowired
SignupServiceI signupService;
@Autowired
ShippingImplementation myShipImplementation;
it dies. The problem is that the fields I defined in XML don't seem to be part of the bean that is autowired into the Controller. For example, I put a breakpoint in the setAddressValidationUri
and I can see that it is called on application startup with the proper property. However, after the autowiring in the controller the instance of the bean has null for these values.
它死了。问题是我在 XML 中定义的字段似乎不是自动装配到控制器中的 bean 的一部分。例如,我在 中放置了一个断点setAddressValidationUri
,我可以看到它在具有正确属性的应用程序启动时被调用。但是,在控制器中自动装配之后,bean 的实例对于这些值具有 null。
You will also notice that there's a SignupServiceI
reference... this is a Service bean that does autowire successfully. It's an interface implemented by a class that is annotated with the @Service
tag.
您还会注意到有一个SignupServiceI
引用……这是一个成功执行自动装配的服务 bean。它是一个用@Service
标签注释的类实现的接口。
采纳答案by M. Deinum
Remove the @Component
.
删除@Component
.
The adding of this annotation combined with component-scanning leads to two instances of the same bean (actually in your case one bean overrides the other, due to the same bean name/id). You have also defined it in XML. The one in XML is the one you want to use.
添加此注释与组件扫描相结合会导致同一个 bean 的两个实例(实际上在您的情况下,一个 bean 覆盖另一个 bean,因为 bean name/id 相同)。您还在 XML 中定义了它。XML 中的那个就是您要使用的那个。