|
| 1 | +<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | +<beans xmlns="http://www.springframework.org/schema/beans" |
| 3 | + xmlns:context="http://www.springframework.org/schema/context" |
| 4 | + xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 5 | + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> |
| 6 | + |
| 7 | + <context:property-placeholder location="classpath*:database.properties" /> |
| 8 | + <context:spring-configured /> |
| 9 | + |
| 10 | + <context:component-scan base-package="com.baeldung.multitenancy"> |
| 11 | + </context:component-scan> |
| 12 | + |
| 13 | + <util:map id="mydatasources"> |
| 14 | + <entry key="tenant1" value-ref="source1" /> |
| 15 | + <entry key="tenant2" value-ref="source2" /> |
| 16 | + </util:map> |
| 17 | + |
| 18 | + <bean id="source1" parent="mainDataSource"> |
| 19 | + <property name="url" value="jdbc:mysql://localhost/tenant1" /> |
| 20 | + <property name="username" value="baeldung" /> |
| 21 | + <property name="password" value="spring" /> |
| 22 | + </bean> |
| 23 | + |
| 24 | + <bean id="source2" parent="mainDataSource"> |
| 25 | + <property name="url" value="jdbc:mysql://localhost/tenant2" /> |
| 26 | + <property name="username" value="testuser" /> |
| 27 | + <property name="password" value="password" /> |
| 28 | + </bean> |
| 29 | + |
| 30 | + <bean class="org.apache.commons.dbcp.BasicDataSource" abstract="true" |
| 31 | + destroy-method="close" id="mainDataSource"> |
| 32 | + <property name="driverClassName" value="${database.driverClassName}" /> |
| 33 | + <property name="testOnBorrow" value="true" /> |
| 34 | + <property name="testOnReturn" value="true" /> |
| 35 | + <property name="testWhileIdle" value="true" /> |
| 36 | + <property name="numTestsPerEvictionRun" value="2" /> |
| 37 | + <property name="minEvictableIdleTimeMillis" value="1000000" /> |
| 38 | + <property name="timeBetweenEvictionRunsMillis" value="1000000" /> |
| 39 | + <property name="validationQuery" value="SELECT 1" /> |
| 40 | + </bean> |
| 41 | + |
| 42 | + <bean id="sessionFactory" class="com.baeldung.multitenancy.SessionFactoryBean"> |
| 43 | + <property name="dataSourceMap" ref="mydatasources" /> |
| 44 | + <property name="dataSource" ref="source1" /> |
| 45 | + <property name="hibernateProperties"> |
| 46 | + <map> |
| 47 | + <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> |
| 48 | + <entry key="hibernate.show_sql" value="true" /> |
| 49 | + <entry key="hibernate.multiTenancy" value="DATABASE" /> |
| 50 | + <entry key="hibernate.tenant_identifier_resolver" |
| 51 | + value-ref="currentSessionTenantIdentifierResolver" /> |
| 52 | + <entry key="hibernate.multi_tenant_connection_provider" |
| 53 | + value-ref="simpleMultiTenantConnectionProvider" /> |
| 54 | + </map> |
| 55 | + </property> |
| 56 | + </bean> |
| 57 | + |
| 58 | + <bean id="currentSessionTenantIdentifierResolver" |
| 59 | + class="com.baeldung.multitenancy.implementation.CurrentSessionTenantIdentifierResolver" |
| 60 | + scope="request"> |
| 61 | + <aop:scoped-proxy /> |
| 62 | + </bean> |
| 63 | + |
| 64 | + <bean id="simpleMultiTenantConnectionProvider" |
| 65 | + class="com.baeldung.multitenancy.implementation.SimpleMultiTenantConnectionProvider"> |
| 66 | + <property name="dataSourceMap" ref="datasources" /> |
| 67 | + </bean> |
| 68 | + |
| 69 | + |
| 70 | + <bean id="multitenantService" class="com.baeldung.multitenancy.service.MultitenantService"> |
| 71 | + <property name="sessionFactory" ref="sessionFactory" /> |
| 72 | + </bean> |
| 73 | + |
| 74 | + <tx:annotation-driven mode="aspectj" |
| 75 | + transaction-manager="transactionManager" /> |
| 76 | + |
| 77 | + <bean id="transactionManager" |
| 78 | + class="org.springframework.orm.hibernate4.HibernateTransactionManager"> |
| 79 | + <property name="sessionFactory" ref="sessionFactory" /> |
| 80 | + <property name="autodetectDataSource" value="false" /> |
| 81 | + </bean> |
| 82 | + |
| 83 | + <tx:advice id="transactionServiceDao" transaction-manager="transactionManager"> |
| 84 | + <tx:attributes> |
| 85 | + <tx:method name="exists" propagation="SUPPORTS" |
| 86 | + rollback-for="java.lang.Exception" /> |
| 87 | + <tx:method name="find*" propagation="SUPPORTS" |
| 88 | + rollback-for="java.lang.Exception" /> |
| 89 | + <tx:method name="get*" propagation="SUPPORTS" |
| 90 | + rollback-for="java.lang.Exception" /> |
| 91 | + <tx:method name="*" propagation="REQUIRED" |
| 92 | + rollback-for="java.lang.Exception" /> |
| 93 | + <tx:method name="*Commit" propagation="REQUIRES_NEW" |
| 94 | + rollback-for="java.lang.Exception" /> |
| 95 | + </tx:attributes> |
| 96 | + </tx:advice> |
| 97 | + |
| 98 | + <aop:config> |
| 99 | + <aop:pointcut id="serviceInvocation" |
| 100 | + expression="execution(public com.baeldung.multitenancy.service.MultitenantService.*(..))" /> |
| 101 | + <aop:advisor pointcut-ref="serviceInvocation" |
| 102 | + advice-ref="transactionServiceDao" /> |
| 103 | + </aop:config> |
| 104 | + |
| 105 | +</beans> |
0 commit comments