【发布时间】:2015-12-22 06:39:57
【问题描述】:
我想为我的测试项目使用 dao 和服务层,但由于某种原因,我得到了 java.lang.NullPointerException: null。
这是工作示例:
用户控制器:
public class UserController {
@Autowired
private UserRepository repo;
@RequestMapping(value = "/register",method = RequestMethod.POST)
public User addUser(@RequestBody User user) {
return repo.saveAndFlush(user);
}}
用户存储库:
public interface UserRepository extends JpaRepository<User,String> {
// Optional<User> findOneByLogin(String login);
}
但我将其替换为:我使用以下教程作为指南 Hibernate JPA DAO Example
用户道
public class UserDao implements UserDaoInterface <User,Integer> {
private Session currentSession;
private Transaction currentTransaction;
public org.hibernate.Session openCurrentSession() {
currentSession = getSessionFactory().openSession();
return currentSession;
}
public org.hibernate.Session openCurrentSessionwithTransaction() {
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSession() {
currentSession.close();
}
public void closeCurrentSessionwithTransaction() {
currentTransaction.commit();
currentSession.close();
}
private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public Session getCurrentSession() {
return currentSession;
}
public void setCurrentSession(Session currentSession) {
this.currentSession = currentSession;
}
public Transaction getCurrentTransaction() {
return currentTransaction;
}
public void setCurrentTransaction(Transaction currentTransaction) {
this.currentTransaction = currentTransaction;
}
public UserDao() {
}
@Override
public void create(User user) {
getCurrentSession().save(user);
}
会不会是会话返回 null?为什么以及如何解决这个问题?
用户服务
public class UserService {
private static UserDao userDao;
public UserService (){
userDao = new UserDao();
}
public void save (User user){
userDao.openCurrentSessionwithTransaction();
userDao.create(user);
userDao.closeCurrentSessionwithTransaction();
}
}
最后 用户控制器
公共类用户控制器 {
private UserService userService;
@RequestMapping(value = "/register",method = RequestMethod.POST)
public void addUser(@RequestBody User user) {
userService.save(user);
}}
我不知道这是否与此有关,但我也在错误日志中得到了这个
at demo.security.SecurityConfiguration$1.doFilterInternal(SecurityConfiguration.java:119)
公共类 SecurityConfiguration 扩展 WebSecurityConfigurerAdapter {
//For more information check spring documentation
private Filter csrfHeaderFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null
&& !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response); // this is line 119
}
};
}}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/login5
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
entitymanager.packages.to.scan=com.spr.model
** 编辑 **
所以我按照 Анатолій Вакалюк 的建议做了 添加了@Repository 和@Service 注解,所以spring 可以管理这个。
但现在我收到以下错误:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
我正在使用 Spring Boot,我需要为此使用 xml 配置吗?
【问题讨论】:
-
您没有在 UserController 中使用
@Autowired注释标记 userService 字段?
标签: java spring hibernate spring-boot spring-data