1

I have a webmodule with JSF 2 end Spring 4.3. In a backing bean I use @Autowired for DI of a service of a JAR. In EAR module there are WAR, JAR with @Service Spring and JAR with Spring configuration file.

Below a web.xml snippet:

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

When I Use @Autowired(required=null) in a Backing Bean the value is null (there is not any exception). My JSF bean

@Component
@ManagedBean
@ViewScoped
public class PortfolioController {


    @Autowired(required = true)
    private PortfolioService portfolioService;

...

Can you help me, please.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Michel Foucault
  • 1,724
  • 3
  • 25
  • 48

2 Answers2

3

PortfolioController is considered a JSF context bean adding @Component to @ManagedBean is totally wrong you can't mark same class as bean in two different contexts (JSF and Spring ).

Two solutions either make PortfolioController a spring bean thus remove the @ManagedBean and @ViewScoped or inject PortfolioController via JSF injection annotation @ManagedProperty

@ManagedProperty("#{portfolioService}")
private PortfolioService portfolioService;
Youans
  • 4,801
  • 1
  • 31
  • 57
  • Ok, I have deleted `@Autowired` with `@ManagedBean`. The implementation service is detected now, but I have this error: `Cannot convert com.ipdb.service.portfolio.impl.PortfolioServiceImpl@5276229c of type class com.ipdb.service.portfolio.impl.PortfolioServiceImpl to interface com.ipdb.service.PortfolioService`. `PortfolioServiceImpl` implements `PortfolioService`. Can you help me please? – Michel Foucault Jul 24 '16 at 16:11
  • I solved, it was a dependacy problem in pom ;) – Michel Foucault Jul 24 '16 at 16:21
0

if the applicationContext.xml is in your jar dependency, then you need to add asterisk after classpath:

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

With the asterisk spring search files applicationContext.xml anywhere in the classpath not only the current project.

Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38