ree*_*sky 5 repository transactional nullpointerexception autowired 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> {
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个定义的服务,其中包含自动装配的存储库:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试以显示我的问题:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
测试在以下行中失败:
assertNotNull(userService.userRepository);
Run Code Online (Sandbox Code Playgroud)
如果我删除服务上的@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>
Run Code Online (Sandbox Code Playgroud)
也许你有一些想法,如何找到错误?
| 归档时间: |
|
| 查看次数: |
3775 次 |
| 最近记录: |