整合JSF 2.1和Spring 3.2

4

我希望在我的JSF应用程序中使用Spring Beans,让Spring将我的服务/存储库注入到我的JSF管理的Bean中。

我在互联网上找到了很多解决方案,但唯一有效的方案是以下代码:

ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
albumRepository = (AlbumRepository) ctx.getBean("albumRepository");

albumRepository是我想要注入的Spring Bean。

问题在于它非常繁琐,我不想在每个类中都这样做,并进行每次注入。我想使用注释,比如“@Inject”。

在Google上搜索答案,我发现应该使用以下配置在faces-config.xml中集成JSF和Spring:

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>

然后,我应该能够使用注释“@ManagedProperty(value =“#{albumRepository}”)”来使用我的Spring Beans。 我尝试过了,但总是出现错误“托管bean的属性albumRepository不存在”。
在谷歌上再次搜索后,我发现我可以使用Spring注释来进行我的注入,唯一需要的就是在applicationContext.xml中注册我的托管bean所在的包。 我已经这样做了,但Spring只是忽略我的注释(@Inject和@Autowired,我尝试了两种方法)。
在所有这些失败之后,我尝试停止使用JSF注释(@ManagedBean和@ViewScoped),而是使用Spring注释(@Controller和@Scope)。 现在JSF甚至无法识别这些bean。
我做错了什么?
编辑:我的ApplicationContext.xml
<context:annotation-config/>
        <jpa:repositories base-package="com.ae.repository" />
        <context:component-scan base-package="com.ae.client.web, com.ae.service" />

        <!-- Data Source -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
            <property name="url"><value>jdbc:mysql://localhost:3306/academia</value></property>
            <property name="username"><value>root</value></property>
            <property name="password"><value>root</value></property>
        </bean>

        <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
        </bean>

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter" ref="jpaAdapter" />
            <property name="persistenceXmlLocation" value="/META-INF/persistence-web.xml"/>
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>

编辑:我的web.xml文件

<!-- Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/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>

    <!-- JSF -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

    <!-- Primefaces -->
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

我认为您无法向component-scan base-package提供逗号分隔的软件包列表。 - Corneil du Plessis
2个回答

1
你的 web.xml 文件中是否有这样的上下文参数?
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/*Context.xml</param-value>
    </context-param> 

你能否在web.xml中发送有关Spring的监听器?


感谢您的回答。在我的web.xml文件中,我有以下内容:<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>。它按预期工作,加载了应用程序上下文。我不认为这是问题所在。无论如何,我已经编辑了原始问题,包括我的web.xml文件。 - Tiago Peres França
1
你有检查过吗?http://www.mkyong.com/jsf2/jsf-2-0-managed-bean-x-does-not-exist-check-that-appropriate-getter-andor-setter-methods-exist/ - mstzn
谢谢mstzn,这是一个非常好的链接!那正是问题所在,我没有为我的“albumRepository”设置setter,我认为它可以像Spring一样工作,只需要“@inject”,但我错了。我仍然不知道为什么Spring注释不起作用,但JSF的“@ManagedProperty”对我来说很好。我将其标记为正确答案,因为有评论的存在。 - Tiago Peres França

0

1
谢谢你的答复,Ravi。我真的希望Spring IOC容器可以管理我所有的bean,但是我尝试了所有的方法,它都不起作用。我尝试了每个注释,但不知道为什么,我认为Spring忽略了包含我的JSF管理Bean(com.ae.client.web)的包。我将使用JSF来管理我的Spring bean,因为我的问题出现在我没有为被注入属性定义一个setter上,现在有了“setAlbumRepository”,它运行得很好。 - Tiago Peres França

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接