Spring Namespace Configuration

Introduction

ShardingSphere-JDBC provides official Spring namespace to make convenient for developers to integrate ShardingSphere-JDBC and Spring Framework.

Spring Namespace Configuration Item

Configuration Example

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://shardingsphere.apache.org/schema/shardingsphere/datasource
  7. http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
  8. ">
  9. <bean id="ds0" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
  10. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  11. <property name="url" value="jdbc:mysql://localhost:3306/ds0" />
  12. <property name="username" value="root" />
  13. <property name="password" value="" />
  14. </bean>
  15. <bean id="ds1" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
  16. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  17. <property name="url" value="jdbc:mysql://localhost:3306/ds1" />
  18. <property name="username" value="root" />
  19. <property name="password" value="" />
  20. </bean>
  21. <!-- Rule configurations, please refer to specific rule configuration for more details. -->
  22. <!-- ... -->
  23. <shardingsphere:data-source id="shardingDataSource" data-source-names="ds0,ds1" rule-refs="..." schema-name="sharding_db" >
  24. <props>
  25. <prop key="xxx.xxx">${xxx.xxx}</prop>
  26. </props>
  27. </shardingsphere:data-source>
  28. </beans>

Configuration Item Explanation

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource-5.0.0.xsd

<shardingsphere:data-source />

NameTypeDescription
idAttributeSpring Bean Id
schema-name (?)AttributeJDBC data source alias
data-source-namesAttributeData source name, multiple data source names are separated by commas
rule-refsAttributeRule name, multiple rule names are separated by commas
props (?)TagProperties configuration, Please refer to Properties Configuration for more details