我想在我的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>
你试过@Component – Satya
现在我试过,相同的结果=/ –
我不认为你可以提供一个逗号分隔打包到组件扫描基本软件包的软件包列表。 –