Skip to content

Commit

Permalink
优化:重构AOP对jfinal 的 Controller和interceptor 注入工作,代码更加容易理解。
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Oct 12, 2017
1 parent 43418bc commit 89333b6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.jboot.web.controller.interceptor;
package io.jboot.aop.web;

import com.google.inject.Injector;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import io.jboot.aop.JbootInjectManager;

/**
* Guice容器对controller的自动注入
* 用于对controller的自动注入
* 注意:如果 Controller通过 @Clear 来把此 拦截器给清空,那么此方法(action)注入将会失效
*/
public class GuiceInterceptor implements Interceptor {
public class ControllerInjectInterceptor implements Interceptor {

private Injector injector;

public GuiceInterceptor() {
public ControllerInjectInterceptor() {
injector = JbootInjectManager.me().getInjector();
}

Expand Down
91 changes: 91 additions & 0 deletions src/main/java/io/jboot/aop/web/WebInterceptorInjectHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (c) 2015-2017, Michael Yang 杨福海 ([email protected]).
* <p>
* Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl-3.0.txt
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.jboot.aop.web;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.Action;
import com.jfinal.core.JFinal;
import com.jfinal.handler.Handler;
import com.jfinal.log.Log;
import io.jboot.aop.JbootInjectManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;

/**
* 用于对 jfinal 的拦截器进行注入
*/
public class WebInterceptorInjectHandler extends Handler {
static Log log = Log.getLog(WebInterceptorInjectHandler.class);
static String[] urlPara = {null};


/**
* 用于记录拦截器是否被注入过,
* 拦截器属于单例模式,注入过一次就没必要再次注入了
*/
static HashSet<String> injectFlags = new HashSet<>();


@Override
public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {

if (target.indexOf('.') != -1) {
return;
}

try {
injectInterceptors(target);
} catch (Throwable ex) {
log.error(ex.toString(), ex);
} finally {
next.handle(target, request, response, isHandled);
}


}

/**
* 对所有拦截器进行注入
*
* @param target
*/
private void injectInterceptors(String target) {
Action action = JFinal.me().getAction(target, urlPara);
if (action == null) {
return;
}

Interceptor[] interceptors = action.getInterceptors();
if (interceptors == null || interceptors.length == 0) {
return;
}

//如果注入过了,就没必要再次注入
if (injectFlags.contains(target)) {
return;
}

for (Interceptor interceptor : interceptors) {
JbootInjectManager.me().getInjector().injectMembers(interceptor);
}

injectFlags.add(target);
}


}
8 changes: 6 additions & 2 deletions src/main/java/io/jboot/web/JbootAppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import io.jboot.Jboot;
import io.jboot.aop.web.WebInterceptorInjectHandler;
import io.jboot.component.log.Slf4jLogFactory;
import io.jboot.component.metrics.JbootMetricsManager;
import io.jboot.component.shiro.JbootShiroInterceptor;
Expand All @@ -40,7 +41,7 @@
import io.jboot.utils.ClassNewer;
import io.jboot.utils.ClassScanner;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.controller.interceptor.GuiceInterceptor;
import io.jboot.aop.web.ControllerInjectInterceptor;
import io.jboot.web.controller.interceptor.ParaValidateInterceptor;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.annotation.JFinalSharedMethod;
Expand Down Expand Up @@ -185,7 +186,7 @@ public void configPlugin(Plugins plugins) {
public void configInterceptor(Interceptors interceptors) {


interceptors.add(new GuiceInterceptor());
interceptors.add(new ControllerInjectInterceptor());
interceptors.add(new JbootShiroInterceptor());
interceptors.add(new ParaValidateInterceptor());

Expand All @@ -196,6 +197,9 @@ public void configInterceptor(Interceptors interceptors) {
public void configHandler(Handlers handlers) {
handlers.add(new JbootHandler());

//用于对jfinal的拦截器进行注入
handlers.add(new WebInterceptorInjectHandler());

JbootAppListenerManager.me().onHandlerConfig(handlers);
}

Expand Down
52 changes: 3 additions & 49 deletions src/main/java/io/jboot/web/handler/JbootHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@
*/
package io.jboot.web.handler;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.Action;
import com.jfinal.core.JFinal;
import com.jfinal.handler.Handler;
import com.jfinal.log.Log;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import io.jboot.aop.JbootInjectManager;
import io.jboot.exception.JbootExceptionHolder;
import io.jboot.web.RequestManager;
import io.jboot.web.session.JbootServletRequestWrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;


public class JbootHandler extends Handler {
static Log log = Log.getLog(JbootHandler.class);
static String[] urlPara = {null};

@Override
public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
Expand Down Expand Up @@ -91,50 +85,10 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons
}

private void doHandle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
try {
request.setAttribute("REQUEST", request);
request.setAttribute("CPATH", request.getContextPath());
injectInterceptors(target);
} catch (Throwable ex) {
log.error(ex.toString(), ex);
} finally {
next.handle(target, request, response, isHandled);
}
request.setAttribute("REQUEST", request);
request.setAttribute("CPATH", request.getContextPath());
next.handle(target, request, response, isHandled);
}

/**
* 对所有拦截器进行注入
*
* @param target
*/
private void injectInterceptors(String target) {
Action action = JFinal.me().getAction(target, urlPara);
if (action == null) {
return;
}

Interceptor[] interceptors = action.getInterceptors();
if (interceptors == null || interceptors.length == 0) {
return;
}

//如果注入过了,就没必要再次注入
if (injectFlags.contains(target)) {
return;
}

for (Interceptor interceptor : interceptors) {
JbootInjectManager.me().getInjector().injectMembers(interceptor);
}

injectFlags.add(target);
}

/**
* 用于记录拦截器是否被注入过,
* 拦截器属于单例模式,注入过一次就没必要再次注入了
*/
private static HashSet<String> injectFlags = new HashSet<>();


}

0 comments on commit 89333b6

Please sign in to comment.