自动装配NullPointerException

时间:2015-05-14 05:53:03

标签: java spring hibernate spring-mvc

我在NullPointerException注释时获得@Autowired

我在spring-context.xml中添加了<context:annotation-config/>,而SessionFactory bean id是'sessionFactory'。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">


@Autowired
private SessionFactory sessionFactory;

你能告诉根本原因吗?

堆栈:

java.lang.NullPointerException
at CustomerDao.findAll(CustomerDao.java:20)
at CustomerService.getCustomers(CustomerService.java:19)
at CustomerResource.listCustomers(CustomerResource.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

Spring Bean defination 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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="<package-name>"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>

<!-- Allow static resource to handled by servelet container -->
<mvc:default-servlet-handler />

<context:annotation-config/>
<context:property-placeholder location="classpath:backend.properties" />

<!-- Spring ORM -->

<!--********** Mysql Spring Data Source ********** -->
<bean id="mySQLSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${mysql.jdbc.driverClassName}"></property>
    <property name="url"
        value="jdbc:mysql://${mysql.jdbc.host}:${mysql.jdbc.port}/${mysql.jdbc.databaseName}?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="username" value="${mysql.jdbc.username}" />
    <property name="password" value="${mysql.jdbc.password}" />
</bean>


<!--********** SessionFactory **********-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="mySQLSource"></property>

    <property name="packagesToScan" value="<package-name>.model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
        </props>
    </property>
</bean>

0 个答案:

没有答案