#java #spring #hibernate #spring-security
Вопрос:
Я пытаюсь создать веб-приложение, в разделе безопасности я столкнулся с этой ошибкой:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.name()Ljava/lang/String;
вот applicationContext.xml
lt;?xml version="1.0" encoding="UTF-8"?gt; lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"gt; lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"gt; lt;!-- lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/gt;--gt; lt;property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/gt; lt;!-- lt;property name="driverClassName" value="org.h2.Driver"/gt;--gt; lt;property name="url" value="jdbc:mysql://localhost:3306/mydb"/gt; lt;!-- lt;property name="url" value="jdbc:h2:~/test"/gt;--gt; lt;property name="username" value="root"/gt; lt;property name="password" value="SASHAolic555"/gt; lt;!-- lt;property name="username" value="sa"/gt;--gt; lt;!-- lt;property name="password" value=""/gt;--gt; lt;/beangt; lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"gt; lt;property name="dataSource" ref="dataSource"gt;lt;/propertygt; lt;property name="hibernateProperties"gt; lt;propsgt; lt;prop key="hibernate.dialect"gt;org.hibernate.dialect.MySQL5InnoDBDialectlt;/propgt; lt;!-- lt;prop key="hibernate.dialect"gt;org.hibernate.dialect.MySQLDialectlt;/propgt;--gt; lt;!-- lt;prop key="hibernate.dialect"gt;org.hibernate.dialect.H2Dialectlt;/propgt;--gt; lt;prop key="hibernate.hbm2ddl.auto"gt;updatelt;/propgt; lt;prop key="hibernate.show_sql"gt;truelt;/propgt; lt;prop key="hibernate.format_sql"gt;truelt;/propgt; lt;/propsgt; lt;/propertygt; lt;property name="packagesToScan"gt; lt;listgt; lt;valuegt;com.emusicstorelt;/valuegt; lt;/listgt; lt;/propertygt; lt;/beangt; lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"gt; lt;property name="sessionFactory" ref="sessionFactory"/gt; lt;/beangt; lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"gt; lt;property name="maxUploadSize" value="1024000"/gt; lt;/beangt; lt;security:http auto-config="true"gt; lt;security:intercept-url pattern="/admin/**" access="ROLE_USER" /gt; lt;security:form-login login-page="/login" default-target-url="/admin/" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password" /gt; lt;security:logout logout-success-url="/login?logout" /gt; lt;/security:httpgt; lt;security:authentication-managergt; lt;security:authentication-providergt; lt;security:jdbc-user-service data-source-ref="dataSource" authorities-by-username-query="SELECT username, authority FROM authorities WHERE username = ?" users-by-username-query="SELECT username, password, enabled FROM users WHERE username = ?" /gt; lt;/security:authentication-providergt; lt;/security:authentication-managergt; lt;/beansgt;
это dispatcherServlet.xml
lt;?xml version="1.0" encoding="UTF-8"?gt; lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"gt; lt;context:component-scan base-package="com.emusicstore" /gt; lt;mvc:annotation-driven /gt; lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"gt; lt;property name="prefix" value="/WEB-INF/views/" /gt; lt;property name="suffix" value=".jsp" /gt; lt;/beangt; lt;mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" /gt; lt;tx:annotation-driven/gt; lt;/beans
web.xml
lt;?xml version="1.0" encoding="UTF-8"?gt; lt;web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"gt; lt;absolute-ordering/gt; lt;listenergt; lt;listener-classgt;org.springframework.web.context.ContextLoaderListenerlt;/listener-classgt; lt;/listenergt; lt;context-paramgt; lt;param-namegt;contextConfigLocationlt;/param-namegt; lt;param-valuegt; /WEB-INF/dispatcher-servlet.xml, /WEB-INF/applicationContext.xml lt;/param-valuegt; lt;/context-paramgt; lt;servletgt; lt;servlet-namegt;dispatcherlt;/servlet-namegt; lt;servlet-classgt;org.springframework.web.servlet.DispatcherServletlt;/servlet-classgt; lt;load-on-startupgt;1lt;/load-on-startupgt; lt;/servletgt; lt;servlet-mappinggt; lt;servlet-namegt;dispatcherlt;/servlet-namegt; lt;url-patterngt;/lt;/url-patterngt; lt;/servlet-mappinggt; lt;filtergt; lt;filter-namegt;springSecurityFilterChainlt;/filter-namegt; lt;filter-classgt;org.springframework.web.filter.DelegatingFilterProxylt;/filter-classgt; lt;/filtergt; lt;filter-mappinggt; lt;filter-namegt;springSecurityFilterChainlt;/filter-namegt; lt;url-patterngt;/*lt;/url-patterngt; lt;/filter-mappinggt; lt;/web-appgt;
pom.xml
lt;?xml version="1.0" encoding="UTF-8"?gt; lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"gt; lt;modelVersiongt;4.0.0lt;/modelVersiongt; lt;groupIdgt;com.mywebsitelt;/groupIdgt; lt;artifactIdgt;emusicstorelt;/artifactIdgt; lt;versiongt;1.0-SNAPSHOTlt;/versiongt; lt;buildgt; lt;pluginsgt; lt;plugingt; lt;groupIdgt;org.apache.maven.pluginslt;/groupIdgt; lt;artifactIdgt;maven-compiler-pluginlt;/artifactIdgt; lt;configurationgt; lt;sourcegt;7lt;/sourcegt; lt;targetgt;7lt;/targetgt; lt;/configurationgt; lt;/plugingt; lt;/pluginsgt; lt;/buildgt; lt;dependenciesgt; lt;dependencygt; lt;groupIdgt;org.springframeworklt;/groupIdgt; lt;artifactIdgt;spring-webmvclt;/artifactIdgt; lt;versiongt;4.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.springframeworklt;/groupIdgt; lt;artifactIdgt;spring-corelt;/artifactIdgt; lt;versiongt;4.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.springframeworklt;/groupIdgt; lt;artifactIdgt;spring-ormlt;/artifactIdgt; lt;versiongt;4.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;jstllt;/groupIdgt; lt;artifactIdgt;jstllt;/artifactIdgt; lt;versiongt;1.2lt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;taglibslt;/groupIdgt; lt;artifactIdgt;standardlt;/artifactIdgt; lt;versiongt;1.1.2lt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.hibernatelt;/groupIdgt; lt;artifactIdgt;hibernate-corelt;/artifactIdgt; lt;versiongt;4.0.1.Finallt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.hibernate.javax.persistencelt;/groupIdgt; lt;artifactIdgt;hibernate-jpa-2.0-apilt;/artifactIdgt; lt;versiongt;1.0.1.Finallt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;mysqllt;/groupIdgt; lt;artifactIdgt;mysql-connector-javalt;/artifactIdgt; lt;versiongt;8.0.15lt;/versiongt; lt;scopegt;providedlt;/scopegt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;commons-fileuploadlt;/groupIdgt; lt;artifactIdgt;commons-fileuploadlt;/artifactIdgt; lt;versiongt;1.4lt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;commons-iolt;/groupIdgt; lt;artifactIdgt;commons-iolt;/artifactIdgt; lt;versiongt;2.4lt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;javax.servletlt;/groupIdgt; lt;artifactIdgt;servlet-apilt;/artifactIdgt; lt;versiongt;2.5lt;/versiongt; lt;scopegt;providedlt;/scopegt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.hibernatelt;/groupIdgt; lt;artifactIdgt;hibernate-validatorlt;/artifactIdgt; lt;versiongt;4.2.0.Finallt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;javax.validationlt;/groupIdgt; lt;artifactIdgt;validation-apilt;/artifactIdgt; lt;versiongt;1.0.0.GAlt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.springframeworklt;/groupIdgt; lt;artifactIdgt;spring-contextlt;/artifactIdgt; lt;versiongt;4.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.springframework.securitylt;/groupIdgt; lt;artifactIdgt;spring-security-corelt;/artifactIdgt; lt;versiongt;3.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.springframework.securitylt;/groupIdgt; lt;artifactIdgt;spring-security-weblt;/artifactIdgt; lt;versiongt;3.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.springframework.securitylt;/groupIdgt; lt;artifactIdgt;spring-security-configlt;/artifactIdgt; lt;versiongt;3.1.4.RELEASElt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;junitlt;/groupIdgt; lt;artifactIdgt;junitlt;/artifactIdgt; lt;versiongt;RELEASElt;/versiongt; lt;scopegt;testlt;/scopegt; lt;/dependencygt; lt;/dependenciesgt; lt;/projectgt;
Трассировка стека:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.name()Ljava/lang/String; at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4768) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:696) at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1783) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:293) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:460) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:408) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:293) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) at java.security.AccessController.doPrivileged(Native Method) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408) at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) at sun.rmi.transport.Transport$1.run(Transport.java:200) at sun.rmi.transport.Transport$1.run(Transport.java:197) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:196) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.name()Ljava/lang/String; at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.createRequestMappingInfo(RequestMappingHandlerMapping.java:237) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getMappingForMethod(RequestMappingHandlerMapping.java:191) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getMappingForMethod(RequestMappingHandlerMapping.java:51) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$1.matches(AbstractHandlerMethodMapping.java:175) at org.springframework.web.method.HandlerMethodSelector$1.doWith(HandlerMethodSelector.java:62) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:496) at org.springframework.web.method.HandlerMethodSelector.selectMethods(HandlerMethodSelector.java:57) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:172) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:147) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:126) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) ... 59 more 27-Oct-2021 20:28:32.495 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file 27-Oct-2021 20:28:32.496 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [/eMusicStore] startup failed due to previous errors 27-Oct-2021 20:28:32.514 WARNING [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [eMusicStore] registered the JDBC driver [com.mysql.cj.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. [2021-10-27 08:28:32,525] Artifact eMusicStore:war exploded: Error during artifact deployment. See server log for details. 27-Oct-2021 20:28:34.246 INFO [mysql-cj-abandoned-connection-cleanup] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1427) at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1052) at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkThreadContextClassLoader(AbandonedConnectionCleanupThread.java:117) at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:84) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 27-Oct-2021 20:28:35.295 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [D:apache-tomcat-9.0.52webappsmanager] 27-Oct-2021 20:28:35.517 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:apache-tomcat-9.0.52webappsmanager] has finished in [220] ms
Комментарии:
1. Обычно
NoSuchMethodError
это происходит, когда вы используете разные модули из spring с разными версиями, что, по-видимому, имеет место здесь. Используйте что-то вроде maven или gradle для управления зависимостями и убедитесь, что версии выровнены. В этом случае похоже, что вы пытаетесь перехитрить проекты Spring Boot starter (как вы указываете, вы используете Spring Boot). Пожалуйста, добавьте своеpom.xml
илиbuild.gradle
к вопросу.2. Вам следует использовать более новую версию Spring Security или более старую версию Spring. Spring Security 3.1 не был разработан для Spring 4.1 (и, в свою очередь, извлекает более старые версии библиотек Spring, что приводит к этой проблеме).
Ответ №1:
Вам нужно @Autowire SessionFactory, где вы делаете инъекцию. Вот почему IOC не может найти компонент для создания экземпляра компонента.
Комментарии:
1. я делал это раньше !!!
2. можете ли вы поделиться своим классом контроллера
3. Речь идет не о невозможности найти фабрику сеансов, а о несовместимых версиях библиотек.