2013-07-06 59 views
0

我想改变我的数据源动态的,所以我用弹簧3.1和Profile属性,这是我的应用程序上下文XML的一部分:我想知道为什么我的dataSoure无法找到?

<?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> 

web.xml设置spring.profiles.default

<?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 

我不熟悉春天,并试图服务器时间,但也不能正常工作,可以在任何一个可以帮我解决这个问题? 谢谢。

加入我的全部应用程序上下文XML和web.xml

回答

0

我觉得你的背景下PARAM应该

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

变化默认活跃

+0

问题仍然存在,任何其他的方法呢? – znlyj

+0

您可以编辑您的文章,并添加完整的web.xml? –

+0

是的,我添加了我的完整配置文件。 – znlyj

相关问题