2016-07-24 151 views
0

我有一個JSF 2結束Spring 4.3的webmodule。在支持bean中,我使用@Autowired作爲JAR服務的DI。在EAR模塊中有WAR,JAR和@Service Spring和JAR以及Spring配置文件。Spring @Autowired(required = true)爲null

下面一個web.xml片段:

<context-param> 
     <param-name>locatorFactorySelector</param-name> 
     <param-value>classpath:beanRefContext.xml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>parentContextKey</param-name> 
     <param-value>sharedContext</param-value> 
    </context-param> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 
<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 

applicationContext.xml

<context:annotation-config /> 
    <context:spring-configured /> 
<!-- package of @Service class in jar module in EAR-- > 
    <context:component-scan base-package="com.ipdb.service" /> 

beanRefContext.xml:

<bean id="sharedContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg> 
    <list> 
     <value>spring-ctx.xml</value> 
    </list> 
</constructor-arg> </bean> 

當我在後臺bean使用@Autowired(required=null)null(有沒有任何例外)。我的JSF豆

@Component 
@ManagedBean 
@ViewScoped 
public class PortfolioController { 


    @Autowired(required = true) 
    private PortfolioService portfolioService; 

... 

請問你能幫助我嗎?

+1

這不是一個Spring bean和Spring不負責管理或自動裝配它注入PortfolioController。 – chrylis

+0

由於你的問題最初是用[spring-mvc]標記的,下面是一些需要思考的食物:http://stackoverflow.com/q/18744910 – BalusC

回答

2

PortfolioController被認爲是一個JSF背景豆加入@Component@ManagedBean是完全錯誤的,你不能標記同一類豆在兩個不同的上下文(JSFSpring)。

兩個解決方案,要麼使PortfolioController一個Spring bean從而去除@ManagedBean@ViewScoped或通過JSF注入註解@ManagedProperty

@ManagedProperty("#{portfolioService}") 
private PortfolioService portfolioService; 
+0

好吧,我已經用@ ManagedBean刪除了@ @ Autowired。現在檢測到實現服務,但是我有這個錯誤:'不能將類com.ipdb.service.portfolio.impl.PortfolioServiceImpl類型的[email protected]轉換爲接口com.ipdb.service .PortfolioService'。 'PortfolioServiceImpl'實現'PortfolioService'。你能幫我嗎? –

+0

我解決了,這是pom中的一個依賴問題;) –

0

如果applicationContext.xml是在你的罐子依賴,那麼你需要的類路徑後添加星號:

<param-name>contextConfigLocation</param-name> 
     <param-value>classpath*:applicationContext.xml</param-value> 
    </context-param> 

帶星號的彈簧搜索文件applicationContext.xml在classpath不僅是當前項目的任何地方。