2016-07-24 25 views
0

Ich habe ein Webmodul mit JSF 2 Ende Frühling 4.3. In einer Backing-Bean verwende ich @Autowired für DI eines Dienstes einer JAR. Im EAR-Modul gibt es WAR, JAR mit @Service Spring und JAR mit Spring-Konfigurationsdatei.Spring @Autowired (erforderlich = wahr) ist null

Unterhalb eines web.xml Schnipsel:

<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> 

Als ich @Autowired(required=null) in einem Hintergrund-Bean verwenden ist der Wert null (es ist keine Ausnahme). Meine JSF-Bean

@Component 
@ManagedBean 
@ViewScoped 
public class PortfolioController { 


    @Autowired(required = true) 
    private PortfolioService portfolioService; 

... 

Können Sie mir bitte helfen.

+1

Es ist nicht ein Spring-Bean und Frühling ist nicht die Verwaltung oder autowiring es somit entfernen. – chrylis

+0

Da Ihre Frage ursprünglich mit [spring-mvc] getaggt wurde, hier ein paar Denkanstöße: http://stackoverflow.com/q/18744910 – BalusC

Antwort

2

PortfolioController gilt als JSF Kontext Bohne @Component zu @ManagedBean Zugabe ist völlig falsch Sie nicht dieselbe Klasse wie Bohnen in zwei verschiedenen Kontexten (JSF und Spring) markieren können.

Zwei Lösungen entweder machen PortfolioController eine Feder Bohne die @ManagedBean und @ViewScoped oder injizieren PortfolioController über JSF Injektion Anmerkung @ManagedProperty

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

Ok, ich habe '@ Autowired' mit' @ ManagedBean' gelöscht. Der Implementierungsservice wird jetzt erkannt, aber ich habe diesen Fehler: '[email protected] des Typs class com.ipdb.service.portfolio.impl.PortfolioServiceImpl kann nicht in die Schnittstelle com.ipdb.service konvertiert werden .PortfolioService'. 'PortfolioServiceImpl' implementiert' PortfolioService'. Kannst du mir bitte helfen? –

+0

Ich löste, es war ein Abhängigkeitsproblem in Pom;) –

0

wenn die applicationContext.xml in Ihrem Glas Abhängigkeit ist, dann müssen Sie Sternchen nach Classpath hinzufügen:

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

Mit der Sterne Frühjahr Such Dateien applicationContext.xml überall in der Classpath nicht nur das aktuelle Projekt.