有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jsf Spring@Autowired(required=true)为空

我有一个带有JSF2 end Spring 4.3的webmodule。在支持bean中,我使用@Autowired作为JAR服务的DI。EAR模块中有WAR、带有@ServiceSpring的JAR和带有Spring配置文件的JAR

下面是一个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(没有任何异常)。我的JSFbean

@Component
@ManagedBean
@ViewScoped
public class PortfolioController {


    @Autowired(required = true)
    private PortfolioService portfolioService;

...

你能帮帮我吗


共 (1) 个答案

  1. # 1 楼答案

    PortfolioController被认为是一个JSF上下文bean,将@Component添加到@ManagedBean是完全错误的,您不能在两个不同的上下文(JSFSpring)中将同一类标记为bean

    两种解决方案要么使PortfolioController成为spring bean,从而移除@ManagedBean@ViewScoped,要么通过JSF注入注释@ManagedProperty注入PortfolioController

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