关于spring mvc3, mysql, setDataSource, AutoWired <2>
泪奔,两个小时,一个蛋疼的错误。。。
不说具体过程了,为了本地和sae测试,我配置了两个数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_pianohero"></property> <property name="username" value="xxxx"></property> <property name="password" value="xxxx"></property> <property name="maxIdle" value="10"/> <property name="timeBetweenEvictionRunsMillis" value="15000" /> <property name="minIdle" value="5"/> <property name="maxActive" value="100"/> <property name="numTestsPerEvictionRun" value="20" /> <property name="validationQuery" value="SELECT 1 FROM DUAL" /> <property name="removeAbandoned" value="true"/> <property name="maxWait" value="1000"></property> <property name="testWhileIdle" value="true" /> <property name="removeAbandonedTimeout" value="180"/> </bean> <bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/piano_db"></property> <property name="username" value="root"></property> </bean>
然后在java里我直接这么写的,
@Autowired public void setDataSource(DataSource dataSource) { JdbcTemplate template = new JdbcTemplate(dataSource);
@Autowired public void setLocalDataSource(DataSource dataSource) { JdbcTemplate template = new JdbcTemplate(dataSource);
以为他会根据名称自己来选那个,结果不是!!他数据类型对就OK了!!所以要这么写:
@Autowired
@Qualifier("localDataSource")
public void setDataSource(DataSource dataSource)
引以为戒