引起:java.lang.NullPointerException,因为映射了2个xml文件(orm.xml和springConfiguration.xml)

时间:2016-08-29 23:06:38

标签: java spring

我正在使用spring项目(版本4.0.2)。 我分享了以下文件:

我项目的Structure

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <jpa:repositories base-package="com.journaldev.spring.jdbc.dao" />
  
    <bean id="employeeDAO" class="com.journaldev.spring.jdbc.dao.EmployeeDAOImpl">
	<property name="dataSource" ref="dataSource" />
    </bean>
	
    <bean id="employeeDAOJDBCTemplate" class="com.journaldev.spring.jdbc.dao.EmployeeDAOJDBCTemplateImpl">
	<property name="dataSource" ref="dataSource" />
    </bean>
	
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql://localhost:3306/springdb" />
	<property name="username" value="root" />
	<property name="password" value="root" />
    </bean>
	
    <import resource="classpath:META-INF/orm.xml"/>

</beans>

orm.xml中

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
        xmlns="http://java.sun.com/xml/ns/persistence/orm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
                            http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
        version="2.0">
 	
    <named-query name="Employee.findAll">
        <query>SELECT e FROM Employee e</query>
    </named-query>
</entity-mappings>

TodoRepository.java

public interface TodoRepository extends Repository<Employee, Long>
{
    @Query(nativeQuery = true)
    public List<Employee> findAll();
}

TodoRepositoryBean.java

@ManagedBean
@ViewScoped
public class TodoRepositoryBean
{
	@Autowired
	private TodoRepository todoRepository;
	
	public void doSomething(ActionEvent evt) 
	{
		List<Employee> les = todoRepository.findAll();
		for(Employee e : les)
		{
			System.out.println("Employee Name is: " + e.getName());
		}
	}
}

测试按钮

<p:commandButton value="Add" 
                 actionListener="#{todoRepositoryBean.doSomething}"/>

点击此按钮后,会出现此异常:

  

引起:java.lang.NullPointerException       在com.acme.esprit.utils.TodoRepositoryBean.doSomething(TodoRepositoryBean.java:31)

有任何关于解决这个问题的想法吗。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将spring xml配置更改为以下内容。您错过了jpa存储库bean描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <jpa:repositories base-package="your repository package" />
       ....

</beans>