我在调用Spring Bean的任何方法时收到NullPoin异常,因为它似乎没有注入容器中。我无法理解为什么。
特别是Controller正在使用JSF而Beans是Spring Bean:这可能是问题吗?或者只是配置错误?
(简化)代码和配置是:
Context.xml(从根上下文调用)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:plugin="http://www.springframework.org/schema/plugin"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd">
<!--===========LANGUAGE_TO_LOCALE SERVICE CONFIG BEGIN===========-->
<bean
id="languagesCountryLocaleHelper"
class="com.i18n.MyControllerHelper"
scope="request" />
</beans>
JSF COntroller:
@RequestScoped
@Named
public class MyController {
@Autowired
private MyControllerHelper helper;
public void doSomething() {
helper.doSomething ();
}
}
MyControllerHelper:
@Component
public class MyControllerHelper {
public void doSomething() {
// do something useful
}
}
所以,这是简化的案例..你对我的错误在哪里有任何想法吗?
提前谢谢!
答案 0 :(得分:0)
@Autowired
private MyControllerHelper helper = new MyControllerHelper();
更改为
@Autowired
private MyControllerHelper languagesCountryLocaleHelper;
答案 1 :(得分:0)
我通过以下方式解决了注入MyControllerHelper的问题:
helper = AppContext.getBean(MyControllerHelper.class);
之后,bean被实例化并注入并在其后级联所有其他bean。 我认为这是因为JSF控制器的控制器实例对象在一个不同的容器中,现在自动识别Spring Beans。