gpt4 book ai didi

java - 为什么不在控制台中抛出 NullPointerException?

转载 作者:行者123 更新时间:2023-11-30 11:31:43 32 4
gpt4 key购买 nike

我正在使用 Spring MVC 3.2 和 Spring Security 3.1 构建我的 Web 应用程序。对于身份验证,我实现了 UserDetailsS​​ervice 并使用了 DaoAuthenticationProvider。一切正常!

但是我对 userDetailsS​​erviceImpl bean 有问题:当 dbUsernull 时,NullPointerException(在 dbUser.getUsername() 行)不会被抛入控制台。

package net.dntuan.training.mvc.security.authentication;

import java.util.*;

import net.dntuan.training.mvc.dao.UserDao;
import net.dntuan.training.mvc.security.Role;

import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.*;

@Service("userDetailsServiceImpl")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class UserDetailsServiceImpl implements UserDetailsService {

private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

@Autowired
private UserDao userDao;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
net.dntuan.training.mvc.domain.User dbUser = this.userDao.getUserByUsername(username);
logger.debug("dbUser is null? " + (dbUser == null));
List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
roles.add(Role.USER);

User user = new User(dbUser.getUsername(), dbUser.getPassword(), dbUser.getEnable(), true, true, true, roles);
logger.debug("does not reach here");
return user;
}

}

控制台显示如下

[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Creating new transaction with name [net.dntuan.training.mvc.security.authentication.UserDetailsServiceImpl.loadUserByUsername]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' (AbstractPlatformTransactionManager.java:366)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Opened new Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] for Hibernate transaction (HibernateTransactionManager.java:416)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Preparing JDBC Connection of Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] (HibernateTransactionManager.java:426)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Exposing Hibernate transaction as JDBC transaction [org.postgresql.jdbc4.Jdbc4Connection@2826e5d8] (HibernateTransactionManager.java:487)
Hibernate: select this_.id as id1_1_0_, this_.enable as enable2_1_0_, this_.fullname as fullname3_1_0_, this_.password as password4_1_0_, this_.username as username5_1_0_ from public.user this_ where this_.username=?
[DEBUG] [net.dntuan.training.mvc.security.authentication.UserDetailsServiceImpl] - dbUser is null? true (UserDetailsServiceImpl.java:27)
[TRACE] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Triggering beforeCompletion synchronization (AbstractPlatformTransactionManager.java:936)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Initiating transaction rollback (AbstractPlatformTransactionManager.java:844)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Rolling back Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] (HibernateTransactionManager.java:570)
[TRACE] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Triggering afterCompletion synchronization (AbstractPlatformTransactionManager.java:965)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction (HibernateTransactionManager.java:632)

用Eclipse调试后,我觉得好像是Spring默认捕获了异常。
我是 Spring 的新手,所以我的问题是:为什么 Spring 默认会捕获它? Spring 是如何做到这一点的?

最佳答案

仅仅因为您没有看到它被明确提及,并不意味着没有抛出 NPE。它很可能被抛出,导致数据库事务回滚,因为日志消息清楚地表明了这一点。

更新:
Spring 的事务管理基础结构捕获异常(实际上是任何 Throwable)here ,这样做是为了能够实现事务配置所描述的行为(= @Transactional 方法注释的属性)。默认情况下,任何 RuntimeException 都会触发回滚(请参阅 docs on @Transactional settings ),因为这些被认为是不可恢复的错误条件。如果您在 org.springframework.transaction 上启用 trace 级别日志记录,您将能够看到日志中提到的方法引发的 NullPointerException > 包。

关于java - 为什么不在控制台中抛出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012278/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com