在应用中,需要热切换数据源。但发现如下问题:
-
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'swappable' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is null
-
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
配置如下
-
<bean id= "myDataSource1" class = "org.springframework.jndi.JndiObjectFactoryBean" >
-
<property name="jndiName" >
-
<value>java:/ds1</value>
-
</property>
-
</bean>
-
-
<bean id="myDataSource2" class = "org.springframework.jndi.JndiObjectFactoryBean" >
-
<property name="jndiName" >
-
<value>java:/ds2</value>
-
</property>
-
</bean>
-
-
-
<bean id="myTransactionManager" class = "org.springframework.transaction.jta.JtaTransactionManager" >
-
-
<property name="userTransactionName" >
-
<value>UserTransaction</value>
-
</property>
-
-
<property name="transactionManagerName" >
-
<value>java:/TransactionManager</value>
-
</property>
-
-
</bean>
-
-
<bean id="swappableDataSource"
-
class = "org.springframework.aop.target.HotSwappableTargetSource" >
-
<constructor-arg>
-
<ref local="myDataSource1" />
-
</constructor-arg>
-
</bean>
-
-
<bean id="swappable"
-
class = "org.springframework.aop.framework.ProxyFactoryBean" >
-
<property name="targetSource" >
-
<ref local="swappableDataSource" />
-
</property>
-
</bean>
-
<bean id="mySessionFactory1" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
-
<property name="mappingResources" >
-
<list>
-
<value>test/po/impl/Cus.hbm.xml</value>
-
</list>
-
</property>
-
<property name="hibernateProperties" >
-
<props>
-
<prop key="hibernate.dialect" > ${hibernate.dialect} </prop>
-
<prop key="hibernate.show_sql" > true </prop>
-
<prop key="hibernate.generate_statistics" > true </prop>
-
<prop key="hibernate.cache.provider_class" >
-
org.hibernate.cache.EhCacheProvider</prop>
-
</props>
-
</property>
-
<property name="dataSource" >
-
<ref local="swappable" />
-
</property>
-
</bean>
这里的数据源是用应用服务器JNDI提供的,如果用自己的就没有问题。
-
<bean id= "dataSource1" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
-
<property name="driverClassName" value= "${jdbc.driverClassName}" />
-
<property name="url" value= "${jdbc.url1}" />
-
<property name="username" value= "${jdbc.username}" />
-
<property name="password" value= "${jdbc.password}" />
-
</bean>
用应用服务器的JNDI就会出现上面的错误
解决方法---------代理为javax.jdbc.DataSource接口就可以了。