有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Autowire NullPointerException

我在@Autowired处得到NullPointerException注释

我在spring上下文中添加了<context:annotation-config/>。xml和SessionFactorybean 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)

SpringBean定义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) 个答案