0

我想更改我的数据源动态,所以我使用 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.

4

1 回答 1

0

我认为你的上下文参数应该是

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>test</param-value>
</context-param>

默认更改为活动

于 2013-07-06T06:51:04.703 回答