Integrate JSF 2.1 and Spring 3.2

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" /> <!-- 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> 

Edit: My 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> 
+4
source share
2 answers

Does your web.xml have a context parameter like this?

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

You can also send a spring listener to your web.xml

+1
source

If you want the Spring IOC container to manage all your beans. Use one of the @Component, @Named, or javax.annotation.ManagedBean annotations, and you can enter them using @Autowired or @Inject. Remember to use Spring @Scope for any of these.

See Documentation

If you want to use the JSF IOC container with the Spring IOC container, you can inject Spring beans into the JSF bean using @ManagedProperty.

See also:

0
source

Source: https://habr.com/ru/post/1435296/


All Articles