尝试按照http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/处的代码进行操作 但得到一个令人讨厌的空指针异常:
//DAO
public class TestDAO extends SimpleJdbcDaoSupport {
public List<Map> getDomains() {
String query = "select 1 from dual";
return getSimpleJdbcTemplate().queryForList(query);
}
}
//路由
public class RoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return SessionContextHolder.getDBServer();
}
}
//控制器
@Controller
@RequestMapping(value="/test")
public class HandleRequest {
@RequestMapping(method=RequestMethod.GET)
public String handle(Model model){
TestDAO testDAO = new TestDAO();
SessionContextHolder.setDBServer("server1");
List domains = testDAO.getDomains();
model.addAttribute("domains", domains);
return "test1";
}
}
我的豆子
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="springtest" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1"/>
</bean>
<bean id="parentDataSource" class="org.apache.commons.dbcp.BasicDataSource" abstract="true">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="" />
<property name="password" value="" />
<property name="initialSize" value="5"/>
<property name="validationQuery" value="select 1" />
<property name="defaultAutoCommit" value="false" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="maxWait" value="15000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="master" parent="parentDataSource">
<property name="url" value="jdbc:mysql://${db.host.master}/te_admin?autoReconnect=true" />
</bean>
<bean id="server1" parent="parentDataSource">
<property name="url" value="jdbc:mysql://${db.host.server1}/te_db?autoReconnect=true"/>
</bean>
<bean id="server2" parent="parentDataSource">
<property name="url" value="jdbc:mysql://${db.host.server2}/te_db?autoReconnect=true"/>
</bean>
<bean id="server3" parent="parentDataSource">
<property name="url" value="jdbc:mysql://${db.host.server3}/te_db?autoReconnect=true"/>
</bean>
<bean id="masterJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg><ref bean="master"/></constructor-arg>
</bean>
<bean id="testdao" class="springtest.TestDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="springtest.RoutingDataSource">
<property name="targetDataSources">
<map>
<entry key="server1" value-ref="server1"/>
<entry key="server2" value-ref="server2"/>
<entry key="server3" value-ref="server3"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="server3"/>
</bean>
<!-- properties file -->
<context:property-placeholder location="classpath:local.properties"/>
</beans>
这就是错误
java.lang.NullPointerException
springtest.TestDAO.getDomains(TestDAO.java:15)
答案 0 :(得分:2)
那是因为你自己实例化了TestDAO
,而没有使用上下文中定义的bean。您需要将TestDAO
bean注入控制器,例如
@Controller
@RequestMapping(value="/test")
public class HandleRequest {
private @Autowired TestDAO testDao;
@RequestMapping(method=RequestMethod.GET)
public String handle(Model model){
SessionContextHolder.setDBServer("server1");
List domains = testDAO.getDomains();
model.addAttribute("domains", domains);
return "test1";
}
}