英文:
Spring error: nested exception is java.lang.IllegalArgumentException: Class name must not be null
问题
以下是翻译好的部分:
我目前正在进行一个Spring Security项目:一个带有注册和登录功能的Web应用程序。
稍后将给出的代码来自Udemy课程,我正在学习它。
代码在我看来似乎是正确的,但是会抛出如下异常。
(root cause)
org.springframework.beans.factory.UnsatisfiedDependencyException: 通过字段 'userService' 表达的不满足的依赖项创建 bean 时出错;嵌套的异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 通过字段 'userDao' 表达的不满足的依赖项创建 bean 时出错;嵌套的异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 通过字段 'sessionFactory' 表达的不满足的依赖项创建 bean 时出错;嵌套的异常是 org.springframework.beans.factory.BeanCreationException: 创建 bean 时出错 'sessionFactory' 在 com.luv2code.springsecurity.demo.config.DemoAppConfig 中定义:调用 init 方法失败;嵌套的异常是 java.lang.IllegalArgumentException: 类名不能为空
如果您查看抛出的异常,会发现它有多个嵌套的异常,一直到达 DemoAppConfig.class,所以看起来 @Autowired 注解对我来说工作得很好。
对我来说,似乎这个异常的根本问题是由此引起的。
java.lang.IllegalArgumentException: 类名不能为空
然而,我找不到解决这个问题的方法。我找到了一个关于这个问题的帖子,写于2013年。它说我需要导入这些依赖项:
然而,这没有起作用。
由于看起来问题的主要原因在 DemoAppConfig.class 中,我只会复制和粘贴有关连接的类的源代码部分,足以展示它们如何相互连接,除了 DemoAppConfig.class。
以下是代码。
DemoSecurityConfig.class
@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
// UserService 是扩展 UserDetailsService 的接口。
@Autowired
private UserService userService;
UserService.class
public interface UserService extends UserDetailsService{
User findByUserName(String userName);
void save(CrmUser crmUser);
}
UserServiceImpl.class
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
UserDao.class
public interface UserDao {
User findByUserName(String userName);
void save(User user);
}
UserDaoImpl.class
@Repository
public class UserDaoImpl implements UserDao {
// 需要注入会话工厂
@Autowired
private SessionFactory sessionFactory;
以上是你提供的文本的翻译。如果需要更多信息,请随时告诉我,感谢您的支持。
英文:
I am currently working on a Spring Security project: Web-application with registration & log-in functions.
The code that will be given later below is from a Udemy course and I am just learning it.
The code seems correct to me, but it throws an exception like this.
(root cause)
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'demoSecurityConfig': Unsatisfied dependency
expressed through field 'userService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'userServiceImpl': Unsatisfied dependency expressed
through field 'userDao'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'userDaoImpl': Unsatisfied dependency expressed
through field 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'sessionFactory' defined in
com.luv2code.springsecurity.demo.config.DemoAppConfig: Invocation of init
method failed; nested exception is java.lang.IllegalArgumentException:
Class name must not be null
If you look at the thrown exception, it has multiple nested exceptions on and on until it reaches the DemoAppConfig.class, so it looks like the @Autowired annotations work fine to me.
For me, it looks like the fundamental problem of this exception is caused by this.
java.lang.IllegalArgumentException: Class name must not be null
However, I can't find a solution to fix this issue. I found a post that talks about this issue which was written in 2013. It says that I need to import these dependencies
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.0.CI-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.0.CI-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<!-- ... possibly other repository elements ... -->
<repository>
<id>spring-snapshots</id>
<url>https://repo.springsource.org/snapshot</url>
</repository>
</repositories>
However, It does not work.
As it seems that the main cause of the problem is in DemoAppConfig.class, I will only copy and paste the portion of the source code of wired classes, which is enough to show you how they are wired to each other, except the DemoAppConfig.class.
Here is the code.
DemoSecurityConfig.class
@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
//UserService는 UserDetailsService를 extends하는 interface이다.
@Autowired
private UserService userService;
UserService.class
public interface UserService extends UserDetailsService{
User findByUserName(String userName);
void save(CrmUser crmUser);
}
UserServiceImpl.class
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
UserDao.class
public interface UserDao {
User findByUserName(String userName);
void save(User user);
}
UserDaoImpl.class
@Repository
public class UserDaoImpl implements UserDao {
// need to inject the session factory
@Autowired
private SessionFactory sessionFactory;
DemoAppConfig.class
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.luv2code.springsecurity.demo")
@PropertySource("classpath:persistence-oracle.properties")
public class DemoAppConfig {
@Autowired
private Environment env;
private Logger logger = Logger.getLogger(getClass().getName());
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public DataSource securityDataSource() {
ComboPooledDataSource securityDataSource
= new ComboPooledDataSource();
try {
securityDataSource.setDriverClass(env.getProperty("jdbc.driver"));
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
logger.info(">>> jdbc.url=" + env.getProperty("jdbc.url"));
logger.info(">>> jdbc.user=" + env.getProperty("jdbc.user"));
securityDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
securityDataSource.setUser(env.getProperty("jdbc.user"));
securityDataSource.setPassword(env.getProperty("jdbc.password"));
securityDataSource.setInitialPoolSize(getIntProperty("connection.pool.initialPoolSize"));
securityDataSource.setMinPoolSize(getIntProperty("connection.pool.minPoolSize"));
securityDataSource.setMaxPoolSize(getIntProperty("connection.pool.maxPoolSize"));
securityDataSource.setMaxIdleTime(getIntProperty("connection.pool.maxIdleTime"));
return securityDataSource;
}
private int getIntProperty(String propName) {
String propVal = env.getProperty(propName);
int intPropVal = Integer.parseInt(propVal);
return intPropVal;
}
private Properties getHibernateProperties() {
// set hibernate properties
Properties props = new Properties();
props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
return props;
}
@Bean
public LocalSessionFactoryBean sessionFactory(){
// create session factories
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// set the properties
sessionFactory.setDataSource(securityDataSource());
sessionFactory.setPackagesToScan(env.getProperty("hiberante.packagesToScan"));
sessionFactory.setHibernateProperties(getHibernateProperties());
return sessionFactory;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
// setup transaction manager based on session factory
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
}
Let me know if you need more information, and thank you for your support.
专注分享java语言的经验与见解,让所有开发者获益!
评论