[英]Spring - autowired fields are null
I faced a problem with Spring Framework: @Autowired
are null, but Spring doesn't throw any exceptions, and I can't understand, why are these fields null. 我遇到了Spring Framework的问题:
@Autowired
为null,但是Spring不会引发任何异常,而且我不明白,为什么这些字段为null。
I have a class: 我有一堂课:
package com.processing.gates;
public class Main {
private final ApplicationContext context = Context.getContext();
@Autowired private PaymentGateFactory paymentGateFactory;
@Autowired private CalculatorChooser calculatorChooser;
//...
}
And for example I have the following class: 例如,我有以下课程:
package com.processing.gates.comission;
@Component
public class CalculatorChooser {
//...
}
Here is my Spring configuration xml: 这是我的Spring配置xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.processing.gates"/>
<bean id="logPath" class="java.lang.String"> <!-- путь к папке с логами -->
<constructor-arg value="/home/yoba/NetBeansProjects/processing/gates/log/"/>
</bean>
<!-- ... -->
</beans>
When I try to write in xml: 当我尝试用xml编写时:
<bean id="calculator" class="com.processing.gates.comission.CalculatorChooser"/>
and get it from code: 并从代码中获取它:
CalculatorChooser cc = (CalculatorChooser)Context.getContext().getBean("calculator");
it works fine. 它工作正常。 But @Autowired doesn't work.
但是@Autowired不起作用。 How can I fix this?
我怎样才能解决这个问题? Thanks!
谢谢!
让Spring通过在应用程序上下文文件中声明Main
bean或使用@Component
批注来管理bean,就像您对CalculatorChooser
所做的那样
<bean id="mainBean" class="com.processing.gates.Main"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.