データソースを動的に変更したいので、Spring 3.1 とProfile
属性を使用します。これが私のアプリケーション コンテキスト xml の一部です。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="merDao" class="com.fruit.dao.merdao.MerDao" scope="prototype">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="springUtil" class="com.fruit.util.SpringUtil" />
<bean id="dbCache" class="com.fruit.cache.DBCache">
<property name="dao" ref="merDao" />
</bean>
<bean id="memCache" class="com.fruit.cache.MemCache">
<property name="next" ref="dbCache" />
<property name="cache" ref="memcachedClient"></property>
</bean>
<bean id="cacheManager" class="com.fruit.cache.CacheManager">
<property name="cache" ref="memCache"></property>
<property name="needCache" value="false"></property>
</bean>
<bean id="merOauthService" class="com.fruit.business.MerOauthBusiness"
scope="prototype">
<property name="dao" ref="merDao" />
<property name="cacheManager" ref="cacheManager" />
</bean>
<bean id="merLoginService" class="com.fruit.business.MerLoginBusiness"
scope="prototype">
<property name="cacheManager" ref="cacheManager" />
<property name="dao" ref="merDao"></property>
</bean>
<bean id="merQueryInfoService" class="com.fruit.business.MerQueryInfoBusiness"
scope="prototype">
<property name="cacheManager" ref="cacheManager" />
</bean>
<bean id="merAddService" class="com.fruit.business.MerAddBusiness" scope="prototype">
<property name="cacheManager" ref="cacheManager"></property>
</bean>
<beans profile="develop">
<context:property-placeholder location="classpath:develop.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${mysqlUrl}" />
</bean>
</beans>
<beans profile="test">
<context:property-placeholder location="classpath:test.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${mysqlUrl}" />
<property name="username" value="${mysqlUser}" />
<property name="password" value="${mysqlPasswd}" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="5" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="timeBetweenEvictionRunsMillis" value="120000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnReturn" value="true" />
<property name="testOnBorrow" value="true" />
</bean>
</beans>
</beans>
私は設定spring.profiles.default
しましたweb.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:applicationContext-dao.xml,classpath:sqlMapConfig.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>test</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>test.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
しかし、常に例外があります:
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
私はSpringに精通しておらず、サーバー時間を試しても機能しません。これを修正するのに役立つ人はいますか? ありがとうございました。
完全なアプリケーション コンテキスト xml とweb.xml
.