title: Spring Boot JSF Integration
body: <p>Environment  : </p>
  
  <p>Tomcat 8 </p>
  
  <p>Spring Boot 1.5</p>
  
  <p>JSF 2.2 </p>
  
  <p>Apache MyFaces </p>
  
  <p>Spring MVC </p>
  
  <p>Code  : </p>
  
  <p>I am integrating Spring Boot and JSF 2.2 in Servlet 3.0 environment.</p>
  
  <p>Config Classes :</p>
  
  <p>JSFConfig.java - Config for JSF.</p>
  
  <pre><code>@Configuration
  @ComponentScan({"com.atul.jsf"})
  public class JSFConfig {
  
          @Bean
          public ServletRegistrationBean servletRegistrationBean() {
              FacesServlet servlet = new FacesServlet();
              return new ServletRegistrationBean(servlet, "*.jsf");
          }
  
  }
  </code></pre>
  
  <p>Spring Boot Main Class :</p>
  
  <pre><code>@SpringBootApplication
  @Import({ // @formatter:off 
      JPAConfig.class,
      ServiceConfig.class, // this contains UserServiceImpl.java class.
      WebConfig.class,
      JSFConfig.class,
  })
  public class SpringbootJpaApplication extends SpringBootServletInitializer{
  
      public static void main(String[] args) {
          SpringApplication.run(SpringbootJpaApplication.class, args);
      }
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
              return application.sources(SpringbootJpaApplication.class);
      }
  }
  </code></pre>
  
  <p>Managed Bean : </p>
  
  <p>UserBean.java - Managed Bean for JSF </p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class UserBean implements Serializable{
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private String name;
      @ManagedProperty(value="#{userServiceImpl}")
      private UserServiceImpl userServiceImpl;
  
      public void addUser(){      
          System.out.println("User Gets added "+this.name);       
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
      public UserServiceImpl getUserServiceImpl() {
          return userServiceImpl;
      }
  
      public void setUserServiceImpl(UserServiceImpl userServiceImpl) {
          this.userServiceImpl = userServiceImpl;
      }
  }
  </code></pre>
  
  <p>Facelets : </p>
  
  <p>home.xhtml - home page </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"&gt;
  
      &lt;h:head&gt;
          &lt;title&gt;JSF 2.0 Hello World&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h2&gt;JSF 2.0 Hello World Example - hello.xhtml&lt;/h2&gt;
          &lt;h:form&gt;
             &lt;h:inputText value="#{userBean.name}"&gt;&lt;/h:inputText&gt;
             &lt;h:commandButton value="Submit" action="#{userBean.addUser}"&gt;&lt;/h:commandButton&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>faces-config.xml : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
                version="2.2"&gt;
  
      &lt;application&gt;
          &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt;
      &lt;/application&gt;
      &lt;lifecycle&gt;
          &lt;phase-listener&gt;org.springframework.web.jsf.DelegatingPhaseListenerMulticaster&lt;/phase-listener&gt;
      &lt;/lifecycle&gt;
  
  &lt;/faces-config&gt;
  </code></pre>
  
  <p>Issue :</p>
  
  <p>1)when I submit form in <code>home.xhtml , userBean.addUser</code> gets called. 
  2)<code>userBean.name</code> gets set with values entered by user.
  3)But <code>userServiceImpl</code> is NULL.
  4)Does that mean that Spring and JSF is not getting integrated ? I have also registered <code>SpringBeanFacesELResolver</code> as mentioned in </p>
  
  <pre><code>faces-config.xml 
  </code></pre>
  
  <p>I also tried removing all JSF specific annotations from UserBean.java and used only Spring specific annotations like below -</p>
  
  <pre><code> @Component
      @SessionScoped 
      public class UserBean implements Serializable{
  
          /**
           * 
           */
          private static final long serialVersionUID = 1L;
          private String name;
          @Autowired
          private UserServiceImpl userServiceImpl;
  
  
      }
  </code></pre>
  
  <p>But when I submit form , I am getting target Unreachable error for <code>#{userBean)</code> . That means <code>userBean</code> is not discoverable for Spring </p>
  
  <p>5)Am I missing anything here ? 
  6)I am not using embedded tomcat provided with Spring Boot </p>