在服务bean上使用@Transactional注释时,Spring数据存储库@Autowiring为null

时间:2012-04-04 19:24:20

标签: repository nullpointerexception autowired transactional spring-data

我正在研究spring框架并尝试在我的项目中使用它。但是我在我的服务中使用了spring数据存储库和@Transactional注释时遇到了以下问题。问题是弹簧启动没有例外。稍后当我尝试访问spring数据存储库时,我得到NullPointerException。也许你有一些想法可以帮助我。

我使用spring数据库定义如下:

package net.question.data.repository;

import net.question.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {  
}

然后我定义了一个包含autowired repository的服务:

package net.question.data.service;

import net.question.data.repository.UserRepository;
import net.question.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService {
    @Autowired
    public UserRepository userRepository;

    public void doStuff(User usr) {
            // login will be here
    }
}

这是显示我的问题的测试:

package net.question.spring;

import static org.junit.Assert.assertNotNull;
import net.question.data.service.UserService;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/app-context.xml")
public class InjectionTestSuite {

    @Autowired
    UserService userService;

    @Test
    public void testRepositoryInjection() {
            assertNotNull(userService);
            assertNotNull(userService.userRepository);
    }
}

测试在以下行中失败:

assertNotNull(userService.userRepository);

如果我删除了服务上的@Transactional注释,则测试通过。

这是我的app-context.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd ">

    <jpa:repositories base-package="net.question.data.repository" />

    <!-- For discovering entity services -->
    <context:component-scan base-package="net.question.data.service" />

    <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="hibernate_mysql" />
    </bean>

    <bean id="transactionManager"     class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />
</beans>

也许你有一些想法,如何找到错误?

0 个答案:

没有答案