当将Spring服务bean注入JSF ManagedBean时,它向我显示了一个NullPointerException

时间:2016-03-04 09:23:09

标签: spring jsf dependency-injection

Tomcat 8,JSF,Spring 当我尝试向jsf托管bean注入spring服务时,它显示了NullPointerException

check.xhtml

 <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:p="http://primefaces.org/ui"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"

          >

      <h:head>        </h:head>

      <h:body>

      <p:dataTable var="check" value="#{checkBean.checks}">
        <p:column headerText="Name">
            <h:outputText value="#{check.name}" />
        </p:column>

        <p:column headerText="URL">
            <h:outputText value="#{check.url}" />
        </p:column>

    </p:dataTable>

      </h:body>  
     </html>

CheckBean

 @Setter
    @ManagedBean
    public class CheckBean implements Serializable {


        private static final long serialVersionUID = 1L;

          @ManagedProperty("#{checkService}")
          private CheckService checkService;


        private List<Check> checks;

        private Check check = new Check();

        public void save()
        {   

            checkService.save(check);       
        }


        public List<Check> getChecks() {

            System.out.println(checkService==null);
            //checks=checkService.findAll();
            return checks;
        }
        }

CheckService

@Getter
@Setter
@Service
public class CheckService implements CheckServiceInterface {


    @Autowired
    private CheckRepository checkRepository;



    public List<Check> findAll()
    {
        //return checkRepository.findAll();
        List<Check> l=new ArrayList<Check>();
        Check c =new Check();
        c.setName("aa");
        c.setUrl("www.exemple.com");
        l.add(c);
        return l;


    }


    public void save(Check check) {

        checkRepository.save(check);

    }
}

面-config.xml中

<?xml version="1.0"?>
<faces-config version="2.2" 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
 http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

 <application>

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

</application>
</faces-config>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name>Crud</display-name>
 <context-param>
  <param-name>javax.faces.CONFIG_FILES</param-name>
  <param-value>/WEB-INF/faces-config.xml</param-value>
 </context-param>
 <context-param>
  <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
  <param-value>0</param-value>
 </context-param>
 <context-param>
  <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
  <param-value>true</param-value>
 </context-param>
 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>

 <listener>
  <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
 </listener>


<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>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>selectChecks.xhtml</welcome-file>
 </welcome-file-list>



</web-app>

SpringConfiguration

@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories("com.crud.repositories")
public class SpringConfiguration {


    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://127.0.0.1:3306/test");
        ds.setUsername("crud");
        ds.setPassword("crud");
        return ds;
    }



    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("com.crud.entities");
        return lef;
    }



    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

}

MyWebAppInitializer

公共类MyWebAppInitializer实现了WebApplicationInitializer {

public void onStartup(ServletContext container) throws ServletException {
    // TODO Auto-generated method stub

    AnnotationConfigWebApplicationContext rootContext=new AnnotationConfigWebApplicationContext();
    rootContext.register(SpringConfiguration.class);


    container.addListener(new ContextLoaderListener(rootContext));

}

}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>CrudTest</groupId>
    <artifactId>Crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.9</version>
        </dependency>



        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.3</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>

        </dependency>


        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.7</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.7</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.7.1.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.7.Final</version>
        </dependency>


        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>


        <!--********************************************************************************************** -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.1-b04</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.sun.el</groupId>
            <artifactId>el-ri</artifactId>
            <version>1.0</version>
        </dependency>





        <!--********************************************************************************************** -->

    </dependencies>



    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframewok</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.2.5.RELEASE</version>
                <type>bom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <finalName>JavaServerFaces</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

0 个答案:

没有答案