[英]Autowiring Map from Spring applicationContext into JSF bean
[英]Autowiring a Spring Bean from a JSF Controller
調用Spring Bean的任何方法時都會收到NullPoin異常,因為它似乎沒有注入容器中。 我不明白為什么。
特殊之處在於Controller正在使用JSF,而Bean是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
}
}
所以,這是簡化的情況..您對我的錯誤可能在哪里有任何想法嗎?
先感謝您!
@Autowired
private MyControllerHelper helper = new MyControllerHelper();
換成這個
@Autowired
private MyControllerHelper languagesCountryLocaleHelper;
我解決了通過以下方式注入MyControllerHelper的問題:
helper = AppContext.getBean(MyControllerHelper.class);
之后,實例化並注入該bean,然后級聯所有其他bean。 我想這是由於以下事實造成的:作為JSF Controller的Controller實例對象位於另一個容器中,該容器現在可以自動識別Spring Beans。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.