6

我正在研究 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> {  
}

然后我定义了一个包含自动装配存储库的服务:

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>

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

4

0 回答 0