2016-05-06 153 views
0

我想這http://websystique.com/spring-security/spring-security-4-remember-me-example-with-hibernate/轉換成春,春季安全和MyBatis的,這裏是項目結構,我有: Project Structure嘗試轉換彈簧+彈簧安全+ Hibernate項目去春來+彈簧安全+ MyBatis的項目

由於我使用Java配置,並且我想擺脫xml文件。但是,當我運行該項目,我得到了以下幾個問題:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.security.core.userdetails.UserDetailsService com.vincent.configuration.SecurityConfiguration.userDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUserDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vincent.service.UserService com.vincent.service.CustomUserDetailsService.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vincent.dao.UserDAO com.vincent.service.UserServiceImpl.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vincent.mapper.UserMapper com.vincent.dao.UserDAOImpl.userMapper; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [V:\Programming\SpringMVC\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\SpringSecurityTest\WEB-INF\classes\com\vincent\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': Error creating bean with name 'sqlSessionFactory' defined in com.vincent.configuration.DatabaseConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in com.vincent.configuration.DatabaseConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NullPointerException 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4736) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 

(後越來越多的日誌)

我試圖解決這個問題,但我不知道如何解決....

完整的代碼是:https://github.com/zhengye1/SpringSecurityTest

更新時間: 後,我嘗試llya提供的解決方案,我得到了新的異常

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.security.core.userdetails.UserDetailsService com.vincent.configuration.SecurityConfiguration.userDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUserDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vincent.service.UserService com.vincent.service.CustomUserDetailsService.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vincent.dao.UserDAO com.vincent.service.UserServiceImpl.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vincent.mapper.UserMapper com.vincent.dao.UserDAOImpl.userMapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [V:\Programming\SpringMVC\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\SpringSecurityTest\WEB-INF\classes\com\vincent\mapper\UserMapper.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ibatis/cursor/Cursor 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4736) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 
+0

注意:如果添加@MapperScan(basePackages =「com.vincent.mapper」),則不需要mapperScan bean方法。他們都做同樣的工作。你如何開始這個應用程序? – Lucky

+0

@Lucky我把項目拖到Eclipse中的Tomcat服務器上,並從Eclipse開始 –

回答

0

您有NPE原因環境不是自動裝配的。我不知道確切的原因,但你可以做workaraound如下(got from here):

@Configuration 
@EnableTransactionManagement 
@ComponentScan({ "com.vincent.configuration" }) 
@MapperScan(basePackages="com.vincent.mapper") 
@PropertySource(value = { "classpath:application.properties" }) 
public class DatabaseConfig implements EnvironmentAware { 

    private Environment environment; 


    @Override 
    public void setEnvironment(Environment arg0) { 
     this.environment = arg0; 
    } 
    //skipped code here 
} 

UPD
還有另一種情況例外:ClassNotFoundException: org.apache.ibatis.cursor.Cursor因爲org.mybatis:mybatis-spring:jar:1.3.0需要org.mybatis:mybatis:jar:3.4.0,所以從3.1.1升級org.mybatis:mybatis3.4.0
將會有一個更加異常Failed to parse expression 'isAuthenticated())'原因有不必要的右括號)'isAuthenticated())'SecurityConfiguration:49

+0

我試試你提供的方式,但好像仍然得到相同的錯誤 –

+0

試試(除了增加EnvironmentAware)添加'@Import(DatabaseConfig。類)'註釋OwnWebMVCConfiguration – Ilya

+0

試過了,但仍然沒有工作.. –

0

你添加您的CurrentUserDetailsS​​ervice類的「@服務」標籤開始?

@Service 
public class CurrentUserDetailsService implements UserDetailsService 
+0

通過此代碼,我實際上添加了註釋..https://github.com/zhengye1/SpringSecurityTest/blob/master/src/main/java/com/vincent/service/CustomUserDetailsS​​ervice.java –