首页 > 代码库 > 使用maven多模块来构建系统时,spring初始化报错的问题

使用maven多模块来构建系统时,spring初始化报错的问题

最近在实验maven结构的maven工程时,碰到一个问题,springbean总是初始化失败:
Related cause:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userMapper‘ defined in file [D:\workspace\mavenweb\mavenweb-webapp\src\main\webapp\WEB-INF\classes\cn\telchina\standard\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory‘: : Error creating bean with name ‘sqlSessionFactory‘ defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [D:\workspace\mavenweb\mavenweb-webapp\target\classes\sql-mappers\account-sql-mapper.xml]‘; nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for cn.telchina.standard.mapper.UserMapper.getUser; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [D:\workspace\mavenweb\mavenweb-webapp\target\classes\sql-mappers\account-sql-mapper.xml]‘; nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for cn.telchina.standard.mapper.UserMapper.getUser    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1199)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1091)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)    at org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean(AbstractBeanFactory.java:1356)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:710)    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:519)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:343)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:320)    at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:187)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:861)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)    at org.apache.catalina.startup.Embedded.start(Embedded.java:825)    at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)    at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:601)    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

直接打成war包,然后放置到tomat中运行没有问题,但是在eclipse中使用Tomcat7:run报错。

一开始非常不理解:从网络上排查,说是bean多次定义或者bean Name重复定义导致,但是由于我能在tomcat中正常运行,因此可以排除。

个人感觉是maven的某个地方配置有问题:

尝试在pom文件中加入:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-resources-plugin</artifactId>    <version>2.6</version>    <configuration>         <overwrite>true</overwrite>    </configuration></plugin>

发现没有效果:

实验使用jettyplugin插件:

<plugin>          <groupId>org.mortbay.jetty</groupId>          <artifactId>jetty-maven-plugin</artifactId>          <version>8.1.15.v20140411</version>  </plugin>

发现还是没有效果。

 

最后在修改applicationcontext.xml后,发现跟有缓存一样,不起作用,进一步排查发现:

在WEB-INF中:有classes目录和lib目录。(从其他项目copy过来后,没有删除)。

删除后问题解决。



作者:sdjnzqr
出处:http://www.cnblogs.com/sdjnzqr/
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任