2012-09-20 60 views
4

我想在我的JSF应用程序中使用我的Spring Bean,让Spring将我的服务/存储库注入到JSF Managed Beans中。集成JSF 2.1和Spring 3.2

我发现了很多在互联网的解决方案,而只是工作的一个是下面的代码行:

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

albumRepository是Spring Bean的我试图注入。

问题是,这真的很蹩脚,我不想在每个班级为每次注射做到这一点。我想使用“@Inject”这样的替代。

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

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

然后,我应该能够使用我的Spring Bean与注释“@ManagedProperty(value =”#{albumRepository}“)”)。我试过了,但我总是得到错误“管理bean的属性albumRepository不存在”。

在Google中重新搜索我发现我可以使用Spring注释来注入,我唯一需要注册的地方是我的托管bean位于applicationContext.xml中。我已经做到了,但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> 
+0

你试过@Component – Satya

+0

现在我试过,相同的结果=/ –

+0

我不认为你可以提供一个逗号分隔打包到组件扫描基本软件包的软件包列表。 –

回答

1

在web.xml有背景PARAM这样吗?在web.xml

+0

谢谢你的回答。在我的web.xml中,我有: ​​contextConfigLocation /WEB-INF/applicationContext.xml 。它按预期工作,它加载应用程序上下文。我不认为这是问题。无论如何,我编辑了原始问题以包含我的web.xml。 –

+1

你检查过吗?存在/ – mstzn

+0

谢谢mstzn,很棒的链接!那正是问题所在,我没有为我的“albumRepository”设置一个setter,我认为它像Spring一样工作,“@inject”就足够了,但我错了。我仍然不知道为什么Spring注释不起作用,但JSF的“@ManagedProperty”对我很好。由于评论,我正在标记为正确的答案。 –

0

如果你想Spring IoC容器管理所有的豆类

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

也可以发送监听春天有关的。使用@Component,@Named或javax.annotation.ManagedBean注释之一,并且可以使用@Autowired或@Inject注入它们。不要忘记为任何这些人使用Spring的@Scope。

Documentation

如果你想使用JSF IOC容器以及与Spring IoC容器一起,你可以Spring Bean注入到使用@ManagedProperty一个JSF豆。

另请参见:

+1

谢谢你的回答Ravi。我真的很喜欢Spring IOC容器来管理我所有的bean,但我尝试了所有的东西,但它不起作用。我尝试了所有可能的注释。我不知道为什么,但我认为Spring忽略了包含我的JSF Managed Beans(com.ae.client.web)的包。我将使用JSF来管理我的Spring bean,我的问题发生了,因为我没有为注入属性定义setter,现在我有一个“setAlbumRepository”,它工作正常。 –