Skip to content

Commit 3bf99c6

Browse files
author
shi.pengyan
committed
提供@master@slave便于主写、从读,使用前必须厘清使用场景;如需混合使用,则需使用DataSourceContextUtil.setContextType(xxx)以及remove方法
1 parent 42286e2 commit 3bf99c6

File tree

9 files changed

+201
-4
lines changed

9 files changed

+201
-4
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.spy.mvc.mybatis.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
8+
import com.spy.mvc.mybatis.service.UserService;
9+
10+
/**
11+
* @author spy
12+
*
13+
* @datetime 2015年10月23日 下午3:21:49
14+
*/
15+
@Controller
16+
@RequestMapping("/test")
17+
public class IndexController {
18+
19+
@Autowired
20+
private UserService UserService;
21+
22+
@RequestMapping("/test")
23+
@ResponseBody
24+
public String test() {
25+
26+
return "this is body";
27+
}
28+
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.spy.mvc.mybatis.datasource.annotation;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.After;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
import org.aspectj.lang.annotation.Pointcut;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.core.annotation.Order;
11+
import org.springframework.stereotype.Component;
12+
13+
import com.spy.mvc.mybatis.common.Const;
14+
import com.spy.mvc.mybatis.datasource.DataSourceContextHolder;
15+
16+
/**
17+
* @author spy
18+
*
19+
* @datetime 2015年10月23日 下午2:26:38
20+
*/
21+
@Aspect
22+
@Component
23+
@Order(0) //优先于@Transactional
24+
public class DataSourceMasterAspect {
25+
private static final Logger logger = LoggerFactory.getLogger(DataSourceMasterAspect.class);
26+
27+
@Pointcut("@annotation(com.spy.mvc.mybatis.datasource.annotation.Master)")
28+
public void dataSourceMaster() {
29+
}
30+
31+
@Before(value = "dataSourceMaster()")
32+
public void before(JoinPoint jp) {
33+
logger.debug("set datasource context type master");
34+
35+
DataSourceContextHolder.setContextType(Const.DATASOURCE_MASTER);
36+
}
37+
38+
@After(value = "dataSourceMaster()")
39+
public void after(JoinPoint jp) {
40+
logger.debug("remove datasource context type master");
41+
42+
DataSourceContextHolder.removeContextType();
43+
}
44+
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.spy.mvc.mybatis.datasource.annotation;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.After;
6+
import org.aspectj.lang.annotation.Around;
7+
import org.aspectj.lang.annotation.Aspect;
8+
import org.aspectj.lang.annotation.Before;
9+
import org.aspectj.lang.annotation.Pointcut;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.core.annotation.Order;
13+
import org.springframework.stereotype.Component;
14+
15+
import com.spy.mvc.mybatis.common.Const;
16+
import com.spy.mvc.mybatis.datasource.DataSourceContextHolder;
17+
18+
/**
19+
* @author spy
20+
*
21+
* @datetime 2015年10月23日 下午2:26:38
22+
*/
23+
@Aspect
24+
@Component
25+
@Order(0)
26+
// 优先于@Transactional
27+
public class DataSourceSlaveAspect {
28+
private final static Logger logger = LoggerFactory.getLogger(DataSourceSlaveAspect.class);
29+
30+
@Pointcut("@annotation(com.spy.mvc.mybatis.datasource.annotation.Slave)")
31+
public void dataSourceSlave() {
32+
}
33+
34+
@Around("dataSourceSlave()")
35+
public Object arround(ProceedingJoinPoint jp) throws Throwable {
36+
logger.debug("around");
37+
Object ret = jp.proceed();
38+
logger.debug("will return ret");
39+
return ret;
40+
}
41+
42+
@Before("dataSourceSlave()")
43+
public void before(JoinPoint jp) {
44+
logger.debug("set datasource context type slave");
45+
46+
DataSourceContextHolder.setContextType(Const.DATASOURCE_SLAVE);
47+
}
48+
49+
@After("dataSourceSlave()")
50+
public void after(JoinPoint jp) {
51+
logger.debug("remove datasource context type slave");
52+
53+
DataSourceContextHolder.removeContextType();
54+
}
55+
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.spy.mvc.mybatis.datasource.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* @author spy
11+
*
12+
* @datetime 2015年10月23日 下午2:25:16
13+
*/
14+
@Target(ElementType.METHOD)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
public @interface Master {
18+
19+
}
20+
21+
22+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.spy.mvc.mybatis.datasource.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* @author spy
11+
*
12+
* @datetime 2015年10月23日 下午2:25:23
13+
*/
14+
@Target(ElementType.METHOD)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
public @interface Slave {
18+
19+
}

src/main/java/com/spy/mvc/mybatis/service/impl/UserSerivceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.transaction.annotation.Transactional;
88

99
import com.spy.mvc.mybatis.dao.UserDAO;
10+
import com.spy.mvc.mybatis.datasource.annotation.Slave;
1011
import com.spy.mvc.mybatis.model.User;
1112
import com.spy.mvc.mybatis.service.UserService;
1213

@@ -29,6 +30,8 @@ public User getUserById(long id) {
2930
}
3031

3132
@Override
33+
@Slave
34+
// TODO just for test
3235
public int saveUser(User user) {
3336
return userDAO.addUser(user);
3437
}

src/main/resources/spring/spring-mvc.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
<!-- <mvc:view-controller path="/" view-name="index.jsp" /> -->
2828

2929
<!-- 自动扫描的包名,这里只扫描@Controller -->
30-
<context:component-scan base-package="com.spy.mvc,com.spy.quick" use-default-filters="false">
30+
<context:component-scan base-package="com.spy.mvc.mybatis.controller" use-default-filters="false">
3131
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
32-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
32+
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
3333
</context:component-scan>
3434

3535
<!-- Enables the Spring MVC @Controller programming model -->

src/main/resources/spring/spring.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
4-
xmlns:tx="http://www.springframework.org/schema/tx"
4+
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
55
xsi:schemaLocation="
66
http://www.springframework.org/schema/beans
77
http://www.springframework.org/schema/beans/spring-beans.xsd
88
http://www.springframework.org/schema/context
9-
http://www.springframework.org/schema/context/spring-context.xsd">
9+
http://www.springframework.org/schema/context/spring-context.xsd
10+
http://www.springframework.org/schema/aop
11+
http://www.springframework.org/schema/aop/spring-aop.xsd
12+
">
1013

1114
<!--需要扫描的包,@Controller交给spring-mvc来处理 -->
1215
<context:component-scan base-package="com.spy.mvc.mybatis">
1316
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
1417
</context:component-scan>
1518

19+
<!-- 启动对@AspectJ注解的支持 -->
20+
<aop:aspectj-autoproxy />
21+
<context:annotation-config />
22+
23+
1624
<!-- i18n, 只能放在spring.xml中否则报错 -->
1725
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
1826
<property name="basenames">

src/test/java/com/spy/mvc/mybatis/test/service/DynamicDataSourceTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import com.spy.mvc.mybatis.common.Const;
1111
import com.spy.mvc.mybatis.datasource.DataSourceContextHolder;
12+
import com.spy.mvc.mybatis.datasource.annotation.Master;
13+
import com.spy.mvc.mybatis.datasource.annotation.Slave;
1214
import com.spy.mvc.mybatis.model.User;
1315
import com.spy.mvc.mybatis.service.UserService;
1416

@@ -32,6 +34,19 @@ public void insertSlaveDB() {
3234
logger.debug("ret value {}", ret);
3335
}
3436

37+
// 以下是注解方式
38+
39+
@Test
40+
@Master
41+
public void insertMasterDB() {
42+
userService.saveUser(createUser());
43+
}
44+
45+
@Test
46+
public void insertSlaveDB2() {
47+
userService.saveUser(createUser());
48+
}
49+
3550
private User createUser() {
3651
User user = new User();
3752
Random random = new Random();

0 commit comments

Comments
 (0)