node-web-mvc
Version:
node spring mvc
64 lines (63 loc) • 2.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @CglibAopProxyPostProcesor
* 动态代理,可用于实现aop
*/
const CglibAopProxy_1 = __importDefault(require("../../aop/CglibAopProxy"));
const PointcutAdvisor_1 = __importDefault(require("../../aop/advisor/PointcutAdvisor"));
const Aspect_1 = __importDefault(require("../../aop/annotations/Aspect"));
const Method_1 = __importDefault(require("../../interface/Method"));
const RuntimeAnnotation_1 = __importDefault(require("../../servlets/annotations/annotation/RuntimeAnnotation"));
const ProxyHelper_1 = __importDefault(require("../factory/ProxyHelper"));
const InstantiationAwareBeanPostProcessor_1 = __importDefault(require("./InstantiationAwareBeanPostProcessor"));
const blacklist = {
'constructor': true,
};
class CglibAopProxyPostProcesor extends InstantiationAwareBeanPostProcessor_1.default {
constructor(beanFactory) {
super();
this.aopProxy = new CglibAopProxy_1.default(beanFactory);
}
getAopProxy() {
return this.aopProxy;
}
postProcessAfterInitialization(beanInstance, beanName) {
const hasAspect = RuntimeAnnotation_1.default.hasClassAnnotation(beanInstance.constructor, Aspect_1.default);
if (beanInstance instanceof PointcutAdvisor_1.default || hasAspect) {
return beanInstance;
}
return this.proxyInstance(beanInstance, beanInstance.constructor);
}
proxyInstance(beanInstance, beanType) {
if (!beanInstance) {
return beanInstance;
}
const scope = this;
const proxy = new Proxy(beanInstance, {
get(target, p, receiver) {
const value = Reflect.get(target, p, receiver);
if (ProxyHelper_1.default.isInstanceSymbol(p)) {
return target;
}
if (typeof value === 'function' && !blacklist[p]) {
// 这里之所以使用新建一个Proxy拦截apply是不希望改完返回的value,如果新生成一个value会导致一些标记在函数上的注解无法获取
return new Proxy(value, {
apply(handler, thisArg, args) {
const aopProxy = scope.getAopProxy();
const method = new Method_1.default(handler, beanType);
// 这里之所以不使用thisArg 是因为在类的内部调用对应的函数不希望被代理拦截
return aopProxy.intercept(proxy, target, method, args);
},
});
}
return value;
},
});
return proxy;
}
}
exports.default = CglibAopProxyPostProcesor;