I want to use my Spring Beans in my JSF application, allowing Spring to inject my services / repositories into JSF managed Beans.
I found many solutions on the Internet, but the only lines that worked were the following lines of code:
ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()); albumRepository = (AlbumRepository) ctx.getBean("albumRepository");
albumRepository is the Spring Bean I'm trying to introduce.
The problem is that she is really lame, I do not want to do this in every class, for every injection. I would like to use annotations like "@Inject".
Searching for an answer on Google, I found that I have to integrate JSF and Spring using the following configuration in faces-config.xml:
<application> <el-resolver> org.springframework.web.jsf.el.SpringBeanFacesELResolver </el-resolver> </application>
Then I have to use my Spring Beans with the annotation @ManagedProperty (value = "# {albumRepository}") ". I tried, but I always get the error" The albumRepository property for the managed Bean does not exist ".
A google search again found that I could use Spring annotations to complete my injections, the only thing I would need to register a package where my managed Beans are in applicationContext.xml, I did this, but Spring just ignores my annotations (@ Inject and @Autowired, I tried both).
After all these crashes, I tried to stop using JSF annotations (@ManagedBean and @ViewScoped), instead I used Spring ones (@Controller and @Scope). Now JSF does not even recognize Beans.
What am I doing wrong?
Edit: My ApplicationContext.xml
<context:annotation-config/> <jpa:repositories base-package="com.ae.repository" /> <context:component-scan base-package="com.ae.client.web, com.ae.service" /> <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>
Edit: My web.xml
<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> <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> <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>