工作中,我准备把事务性不强的实体转移到mongodb中,于是有了此文
我的做法
所有 以 *.dao 结尾的 统统视为 Spring data jpa
所有 以 *.mongo.repository 统统视为 spring-data-mongodb
原因:
兼容遗留代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd" default-lazy-init="false"> <!-- Default bean name is 'mongo' --> <!--<mongo:mongo host="192.168.199.78" port="27017" />--> <bean class="com.mongodb.MongoClient" id="mongo"> <constructor-arg value="192.168.199.78" /> <constructor-arg value="27017" /> </bean> <mongo:db-factory id="mongoDbFactory" dbname="mongo_qmbiz" mongo-ref="mongo"/> <!-- by default look for a Mongo object named 'mongo' - default name used for the converter is 'mappingConverter' --> <mongo:mapping-converter id="mappingConverter" base-package="com.qumei.*.mongodb"> <mongo:custom-converters base-package="com.qumei.*.mongodb.converter"> </mongo:custom-converters> </mongo:mapping-converter> <!-- set the mapping converter to be used by the MongoTemplate --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> <constructor-arg name="mongoConverter" ref="mappingConverter"/> <property name="writeConcern" value="SAFE" /> </bean> <!-- MongoDB GridFS Template --> <bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="mongoDbFactory"/> <constructor-arg ref="mappingConverter"/> </bean> <bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener"/> </beans>
当配置时 mongo:repositories,应注意,需要排除所有mongodb包的dao
<!-- Spring Data Mongo 配置--> <mongo:repositories base-package="com.qumei.**.mongodb.repository" > <repository:exclude-filter type="regex" expression="com.qumei.core.*" /> </mongo:repositories>
当配置时 jpa:repositories,应注意,需要排除所有mongodb包的dao
<!-- Spring Data JPA配置 --> <jpa:repositories base-package="com.qumei.**.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"> <repository:exclude-filter type="regex" expression="com.qumei.core.*" /> </jpa:repositories>
jpa:repositories mongo:repositories 对应的dao注意区分开来 最好以不同的结尾或者不同的包名区分
异常日志
17:07:35.900 INFO o.s.b.f.s.DefaultListableBeanFactory[829] - Overriding bean definition for bean 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': replacing [Root bean: class [org.springframework.validation.beanvalidation.LocalValidatorFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.validation.beanvalidation.LocalValidatorFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] 17:07:36.392 ERROR o.s.web.context.ContextLoader[336] - Context initialization failed java.lang.NoClassDefFoundError: org/springframework/data/mapping/model/FieldNamingStrategy at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.7.0_51] at java.lang.Class.privateGetDeclaredMethods(Class.java:2531) ~[na:1.7.0_51] at java.lang.Class.getDeclaredMethods(Class.java:1855) ~[na:1.7.0_51] at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:129) ~[spring-core-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassUtils.isLiteConfigurationCandidate(ConfigurationClassUtils.java:157) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:108) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:278) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:462) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772) [catalina.jar:8.0.15] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196) [catalina.jar:8.0.15] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.15] at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) [catalina.jar:8.0.15] at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) [catalina.jar:8.0.15] at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714) [catalina.jar:8.0.15] at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1585) [catalina.jar:8.0.15] at sun.reflect.GeneratedMethodAccessor109.invoke(Unknown Source) ~[na:na] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51] at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:8.0.15] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:na] at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.7.0_51] at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:463) [catalina.jar:8.0.15] at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:413) [catalina.jar:8.0.15] at sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source) ~[na:na] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51] at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:8.0.15] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:na] at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.7.0_51] at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487) [na:1.7.0_51] at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97) [na:1.7.0_51] at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328) [na:1.7.0_51] at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420) [na:1.7.0_51] at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848) [na:1.7.0_51] at sun.reflect.GeneratedMethodAccessor105.invoke(Unknown Source) ~[na:na] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51] at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) [na:na] at sun.rmi.transport.Transport$1.run(Transport.java:177) [na:na] at sun.rmi.transport.Transport$1.run(Transport.java:174) [na:na] at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_51] at sun.rmi.transport.Transport.serviceCall(Transport.java:173) [na:na] at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556) [na:na] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811) [na:na] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670) [na:na] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_51] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_51] at java.lang.Thread.run(Thread.java:744) [na:1.7.0_51] Caused by: java.lang.ClassNotFoundException: org.springframework.data.mapping.model.FieldNamingStrategy at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1295) ~[catalina.jar:8.0.15] at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1147) ~[catalina.jar:8.0.15] ... 53 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.mapping.model.FieldNamingStrategy缺少 org.springframework.data.mapping.model.FieldNamingStrategy 类,可是明明已经在maven配置了Spring相关依赖
解决:
1 根据类名分析得知此类位于 org.springframework.data » spring-data-commons 下
2 根据生成打包编译的lib 得知 当前使用的jar是 spring-data-jpa-1.6.4.RELEASE spring-data-mongodb-1.6.2.RELEASE spring-data-commons-1.8.4.RELEASE(根据IDEA的分析图得知此包被spring-data-jpa依赖)
3 查看 spring-data-commons-1.8.4.RELEASE 得知jar内没有 org.springframework.data.mapping.model.FieldNamingStrategy
4 进入 http://mvnrepository.com/ 搜索 spring-data-commons 下载新的 spring-data-commons-1.9.2.RELEASE
5 解决冲突
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <exclusions> <exclusion> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.9.2.RELEASE</version> </dependency>
运行,无异常抛出,over
修改与 2015-03-27