博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2 + Spring3 + MyBatis3 动态切换 多数据源
阅读量:5228 次
发布时间:2019-06-14

本文共 17015 字,大约阅读时间需要 56 分钟。

当我在配置  事务的时候 出现 异常了

据说是要 下载 aspectjweaver.jar 包    地址: 

严重: Context initialization failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basicDataSource' defined in ServletContext resource [/WEB-INF/classes/applicationContext_db.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'userTxAdvice' in bean reference for bean property 'adviceBeanName'
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:457)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
 at org.apache.catalina.core.StandardService.start(StandardService.java:525)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'userTxAdvice' in bean reference for bean property 'adviceBeanName'
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
 at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:86)
 at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
 at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:276)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:896)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:868)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:451)
 ... 25 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'userTxAdvice' in bean reference for bean property 'adviceBeanName'
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
 ... 37 more
2013-5-15 14:08:51 org.apache.catalina.core.StandardContext listenerStart
严重: 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 'basicDataSource' defined in ServletContext resource [/WEB-INF/classes/applicationContext_db.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'userTxAdvice' in bean reference for bean property 'adviceBeanName'
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:457)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
 at org.apache.catalina.core.StandardService.start(StandardService.java:525)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'userTxAdvice' in bean reference for bean property 'adviceBeanName'
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
 at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:86)
 at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
 at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:276)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:896)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:868)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:451)
 ... 25 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'userTxAdvice' in bean reference for bean property 'adviceBeanName'
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
 ... 37 more
2013-5-15 14:08:51 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2013-5-15 14:08:51 org.apache.catalina.core.StandardContext start
严重: Context [/ssm] startup failed due to previous errors
2013-5-15 14:08:51 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
2013-5-15 14:08:51 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/ssm] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
2013-5-15 14:08:52 com.mchange.v2.log.MLog <clinit>

 

 

 

摘自:

我是根据以下内容配置成功的!

 

----------------------------------------

转载自:

简介:

        Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性。而这样的方案就会不 同于常见的单一数据实例的方案,这就要程序在运行时根据当时的请求及系统状态来动态的决定将数据存储在哪个数据库实例中,以及从哪个数据库提取数据。 

Spring对于多数据源,以数据库表为参照,大体上可以分成两大类情况: 
  一是,表级上的跨数据库。即,对于不同的数据库却有相同的表(表名和表结构完全相同)。 
  二是,非表级上的跨数据库。即,多个数据源不存在相同的表。 

多数据源实现

        Spring2.x的版本中采用Proxy模式,就是我们在方案中实现一个虚拟的数据源,并且 用它来封装数据源选择逻辑,这样就可以有效地将数据源选择逻辑从Client中分离出来。Client提供选择所需的上下文(因为这是Client所知道 的),由虚拟的DataSource根据Client提供的上下文来实现数据源的选择。 
  具体的实现就是,虚拟的DataSource仅需继承AbstractRoutingDataSource实现determineCurrentLookupKey()在其中封装数据源的选择逻辑。 

配置方法:

1、数据源的名称常量类:

 

[java]
 
  1. /** 
  2.  * 数据源的名称常量类 
  3.  * @author Administrator 
  4.  * 
  5.  */  
  6. public class DataSourceConst {  
  7.     public static final String USERDB = "USERDB";  
  8.     public static final String SCOREDB = "SCOREDB";  
  9. }  

 

 


2、改变数据源名称类:

 

 

[java]
 
  1. /** 
  2.  * 改变数据源的名称类 
  3.  * @author Administrator 
  4.  * 
  5.  */  
  6. public class DataSourceHandle {  
  7.     /** 
  8.      * 线程本地环境 
  9.      */  
  10.     @SuppressWarnings("unchecked")  
  11.     private static final ThreadLocal contextHolder = new ThreadLocal();  
  12.   
  13.     /** 
  14.      * 设置数据源类型 
  15.      * @param dataSourceType 
  16.      */  
  17.     @SuppressWarnings("unchecked")  
  18.     public static void setDataSourceType(String dataSourceType) {  
  19.         contextHolder.set(dataSourceType);  
  20.     }  
  21.   
  22.     /** 
  23.      * 获取数据源类型 
  24.      * @return 
  25.      */  
  26.     public static String getDataSourceType() {  
  27.         return (String) contextHolder.get();  
  28.     }  
  29.   
  30.     /** 
  31.      * 清除数据源类型 
  32.      */  
  33.     public static void clearDataSourceType() {  
  34.         contextHolder.remove();  
  35.     }  
  36. }  


3、动态数据源类:

 

 

[java]
 
  1. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  
  2.   
  3. /** 
  4.  * 动态数据源类 
  5.  * 这个类必须继承AbstractRoutingDataSource, 
  6.  * 且实现方法 determineCurrentLookupKey,该方法返回一个Object,一般是返回字符串 
  7.  * @author Administrator 
  8.  * 
  9.  */  
  10. public class DynamicDataSource extends AbstractRoutingDataSource {  
  11.   
  12.     @Override  
  13.     protected Object determineCurrentLookupKey() {  
  14.         // 在进行DAO操作前,通过上下文环境变量,获得数据源的类型  
  15.         return DataSourceHandle.getDataSourceType();  
  16.     }  
  17. }  

 

4、 spring配置文件中配置多个数据源 

 

[html]
 
  1. <!-- 数据源基本配置 -->  
  2. <bean id="basicDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  3.     <property name="driverClass" value="com.mysql.jdbc.Driver" />  
  4.     <property name="minPoolSize" value="2" />  
  5.     <property name="maxPoolSize" value="20" />  
  6.     <property name="maxIdleTime" value="1800" />  
  7.     <property name="acquireIncrement" value="2" />  
  8.     <property name="maxStatements" value="0" />  
  9.     <property name="initialPoolSize" value="3" />  
  10.     <property name="idleConnectionTestPeriod" value="1800" />  
  11.     <property name="acquireRetryAttempts" value="30" />  
  12.     <property name="breakAfterAcquireFailure" value="true" />  
  13.     <property name="testConnectionOnCheckout" value="false" />  
  14. </bean>  
  15. <!-- csoa dataSource -->  
  16. <bean id="dataSourceUser" parent="basicDataSource">  
  17.     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/userdb" />  
  18.     <property name="user" value="root" />  
  19.     <property name="password" value="admin" />  
  20. </bean>  
  21. <!-- wa dataSource -->  
  22. <bean id="dataSourceScore" parent="basicDataSource">  
  23.     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/scoredb" />  
  24.     <property name="user" value="root" />  
  25.     <property name="password" value="admin" />  
  26. </bean>  
  27. <!-- data source 抽象类 -->  
  28. <bean id="dataSource" class="com.webterren.csoa.util.dbutil.DynamicDataSource">  
  29.     <property name="targetDataSources">  
  30.         <map key-type="java.lang.String">  
  31.             <entry value-ref="dataSourceUser" key="USERDB"></entry>  
  32.             <entry value-ref="dataSourceScore" key="SCOREDB"></entry>  
  33.         </map>  
  34.     </property>  
  35.     <property name="defaultTargetDataSource" ref="dataSourceUser"></property><!-- 默认数据源 -->  
  36. </bean>  
  37.   
  38. <!-- MyBatis SQLSessionFactory -->  
  39. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  40.     <property name="dataSource" ref="dataSource"></property>  
  41.     <property name="configLocation" value="classpath:config/mybatis/Configuration.xml" />  
  42. </bean>  

这里依个人情况而定

 

测试:

1、SQL语句:

 

[html]
 
  1. <select id="countUser" resultType="int">  
  2.     select count(*) from user  
  3. </select>  
  4.   
  5. <select id="countScore" resultType="int">  
  6.     select count(*) from score  
  7. </select>  

 

2、测试方法

 

[java]
 
  1. SqlSession session = null;  
  2. //user  
  3. DataSourceHandle.setDataSourceType(DataSourceConst.USERDB);  
  4. session = this.getSqlSession();  
  5. int countUser = (Integer)session.selectOne("DbTest.countUser","");  
  6. session.close();  
  7. //score  
  8. DataSourceHandle.setDataSourceType(DataSourceConst.SCOREDB);  
  9. session = this.getSqlSession();  
  10. int countScore = (Integer)session.selectOne("DbTest.countScore","");  
  11. session.close();  
posted on
2013-05-15 10:04  阅读(
...) 评论(
...) 收藏

转载于:https://www.cnblogs.com/wuyifu/archive/2013/05/15/3079264.html

你可能感兴趣的文章
js 时间对象方法
查看>>
网络请求返回HTTP状态码(404,400,500)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
图片加载失败显示默认图片占位符
查看>>
【★】浅谈计算机与随机数
查看>>
《代码阅读方法与实现》阅读笔记一
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
Java中正则表达式的使用
查看>>
算法之搜索篇
查看>>
新的开始
查看>>
java Facade模式
查看>>
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>
SpringAop与AspectJ
查看>>
Leetcode 226: Invert Binary Tree
查看>>
http站点转https站点教程
查看>>
解决miner.start() 返回null
查看>>