我目前正在从事Spring Security项目:具有注册和登录功能的Web应用程序。
下面稍后将提供的代码来自Udemy课程,而我只是在学习它。
代码对我来说似乎是正确的,但它会引发这样的异常。
(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
如果您查看抛出的异常,它会不停地出现多个嵌套异常,直到到达DemoAppConfig.class,所以@Autowired批注对我来说工作正常。
对我来说,此异常的根本问题似乎是由此引起的。
java.lang.IllegalArgumentException: Class name must not be null
但是,我找不到解决此问题的解决方案。我发现有一篇有关此问题的文章写于2013年。它说我需要导入这些依赖项
<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>
但是,它不起作用。
看来问题的主要原因是在DemoAppConfig.class中,所以我只会复制和粘贴有线类源代码的一部分,这足以向您展示它们如何相互连接,除了DemoAppConfig.class。
这是代码。
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;
}
}
如果您需要更多信息,请告诉我,谢谢您的支持。