From 18e67ec7a3f805a1b92e81c17b005dbc682ad719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=98=89=E9=98=B3?= Date: Tue, 3 Mar 2020 15:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=BB=E8=BE=91=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../staticproxy/OrderServiceStaticProxy.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/top/fjy8018/designpattern/pattern/structural/proxy/staticproxy/OrderServiceStaticProxy.java b/src/main/java/top/fjy8018/designpattern/pattern/structural/proxy/staticproxy/OrderServiceStaticProxy.java index c272c27..a43947f 100644 --- a/src/main/java/top/fjy8018/designpattern/pattern/structural/proxy/staticproxy/OrderServiceStaticProxy.java +++ b/src/main/java/top/fjy8018/designpattern/pattern/structural/proxy/staticproxy/OrderServiceStaticProxy.java @@ -29,8 +29,16 @@ public class OrderServiceStaticProxy { * @return */ public int save(Order order) { - beforeMethod(); + beforeMethod(order); + int result = orderService.save(order); + + afterMethod(); + + return result; + } + + private void beforeMethod(Order order) { // 通过用户ID对2取模分库 Integer userId = order.getUserId(); int dbRouter = userId % 2; @@ -39,12 +47,6 @@ public class OrderServiceStaticProxy { // 设置DataSource DataSourceContextHolder.setDBType(DB + String.valueOf(dbRouter)); - afterMethod(); - - return orderService.save(order); - } - - private void beforeMethod() { log.info("静态代理before code"); }