本文整理匯總了Java中org.springframework.cglib.proxy.MethodInterceptor類的典型用法代碼示例。如果您正苦於以下問題:Java MethodInterceptor類的具體用法?Java MethodInterceptor怎麽用?Java MethodInterceptor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MethodInterceptor類屬於org.springframework.cglib.proxy包,在下文中一共展示了MethodInterceptor類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: enhanceFactoryBean
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
/**
* Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory
* instead of creating a new instance. These proxies are created only when calling a FactoryBean from
* within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean
* instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing,
* it will not be proxied. This too is aligned with the way XML configuration works.
*/
private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory,
final String beanName) throws InstantiationException, IllegalAccessException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(fbClass);
enhancer.setUseFactory(false);
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getObject") && args.length == 0) {
return beanFactory.getBean(beanName);
}
return proxy.invokeSuper(obj, args);
}
});
return enhancer.create();
}
示例2: wrapWithIfaceImplementation
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
@SuppressWarnings("unchecked")
static <T> T wrapWithIfaceImplementation(final Class<T> iface, final Specification<Object> targetSpec) {
Enhancer enhancer = new Enhancer();
enhancer.setInterfaces(new Class[] { iface });
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if ("toString".equals(method.getName())) {
return iface.getSimpleName() + "[" + proxy.invoke(targetSpec, args) + "]";
}
return proxy.invoke(targetSpec, args);
}
});
return (T) enhancer.create();
}
示例3: createProxy
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private static <T> T createProxy(final Class<?> classToMock, final MethodInterceptor interceptor) {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(classToMock);
enhancer.setCallbackType(interceptor.getClass());
final Class<?> proxyClass = enhancer.createClass();
Enhancer.registerCallbacks(proxyClass, new Callback[] { interceptor });
return (T) ObjenesisHelper.newInstance(proxyClass);
}
示例4: wrap
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
public static NamedParameterJdbcOperations wrap(final NamedParameterJdbcOperations jdbcOperations) {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(NamedParameterJdbcOperations.class);
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
final Stopwatch sw = Stopwatch.createStarted();
try {
final String methodName = method.getName();
if (methodName.startsWith("query")
|| methodName.startsWith("batchUpdate")
|| methodName.startsWith("update")) {
logSqlQuery(args);
final Object result = method.invoke(jdbcOperations, args);
if (sw.elapsed(TimeUnit.SECONDS) > 5) {
LOG.warn("SQL execution took too long {}", sw);
}
return result;
}
return method.invoke(jdbcOperations, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
});
return (NamedParameterJdbcOperations) enhancer.create();
}
示例5: createProxyObject
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T createProxyObject(Class<T> clazz, Interceptor handler) {
MethodInterceptor methodInterceptor = new ProxyObjectMethodImpl(handler);
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(methodInterceptor);
return (T) enhancer.create();
}
示例6: postProcessBeanFactory
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory context) throws BeansException {
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) {
@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return beanDefinition.getMetadata().isInterface();
}
};
scanner.addIncludeFilter(new AnnotationTypeFilter(Accessor.class));
for (BeanDefinition bd : scanner.findCandidateComponents(basePackage)) {
Class<?> accessorCls;
try {
accessorCls = Class.forName(bd.getBeanClassName());
} catch (ClassNotFoundException e) {
throw new AssertionError(e);
}
log.info("Creating proxy accessor: " + accessorCls.getName());
MethodInterceptor interceptor = new MethodInterceptor() {
private final Lazy<?> target = new Lazy<>(() -> {
log.info("Creating actual accessor: " + accessorCls.getName());
Session session;
if (AccessorScannerConfigurer.this.session == null)
session = mainContext.getBean(Session.class);
else
session = AccessorScannerConfigurer.this.session;
MappingManager mappingManager = new MappingManager(session);
return mappingManager.createAccessor(accessorCls);
});
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if ("toString".equals(method.getName())) {
return accessorCls.getName();
}
return method.invoke(target.get(), args);
}
};
Enhancer enhancer = new Enhancer();
enhancer.setInterfaces(new Class<?>[] { accessorCls });
enhancer.setCallback(interceptor);
Object bean = enhancer.create();
String beanName = StringUtils.uncapitalize(accessorCls.getSimpleName());
context.registerSingleton(beanName, bean);
log.info("Bean registed, name=" + beanName + ", bean=" + bean.toString());
}
}
示例7: asynchronous_publishing
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
@Test
public void asynchronous_publishing() throws InterruptedException {
final String docId = randomUUID().toString();
AbstractVersionStoreJdbc<String, String, ?, ?, ?> originalStore = getStore();
StoreOptions<String, String, ?> options = originalStore.options.toBuilder().publisherType(ASYNC).build();
/*
* Override doPublish to block publish until it is verified that inserted version is not returned
*/
CountDownLatch beforePublish = new CountDownLatch(1);
CountDownLatch afterPublish = new CountDownLatch(1);
MethodInterceptor interceptor = (Object o, Method method, Object[] args, MethodProxy methodProxy) -> {
if (method.getName().equals("publish")) {
beforePublish.await();
try {
return methodProxy.invokeSuper(o, args);
} finally {
afterPublish.countDown();
}
}
return methodProxy.invokeSuper(o, args);
};
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(originalStore.getClass());
enhancer.setCallback(interceptor);
VersionStore<String, String> store = (VersionStore<String, String>) enhancer.create(new Class[] { options.getClass() }, new Object[] { options });
final ObjectVersionGraph<String> originalGraph = ObjectVersionGraph.init(ObjectVersion.<String>builder(rev1).build());
transactionTemplate.execute(status -> {
UpdateBatch<String, String> batch = store.updateBatch(docId);
batch.addVersion(docId, originalGraph.getVersionNode(rev1));
batch.execute();
return null;
});
// getGraphs(Collection) should not return version before it's published
GraphResults<String, String> results = store.getGraphs(asList(docId));
assertThat(results.isEmpty()).isTrue();
// Publish and verify that published version is found
beforePublish.countDown();
afterPublish.await();
results = store.getGraphs(asList(docId));
ObjectVersionGraph<String> graph = results.getVersionGraph(docId);
assertThat(graph.getVersionNode(rev1).getRevision()).isEqualTo(rev1);
}
示例8: allow_cglib_proxy
import org.springframework.cglib.proxy.MethodInterceptor; //導入依賴的package包/類
@Test
public void allow_cglib_proxy() {
final String docId = "docId";
final Revision revision = new Revision();
Function<ObjectVersionGraph<String>, Predicate<VersionNode<PropertyPath, Object, String>>> keep = g -> n -> false;
Map<String, List<Object>> interceptedCalls = new HashMap<>();
MethodInterceptor interceptor = (Object o, Method method, Object[] args, MethodProxy methodProxy) -> {
interceptedCalls.put(method.getName(), ImmutableList.copyOf(args));
return null;
};
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(getStore().getClass());
enhancer.setCallback(interceptor);
@SuppressWarnings("unchecked")
VersionStore<String, String> store = (VersionStore<String, String>) enhancer.create();
store.publish();
assertThat(interceptedCalls.get("publish")).isEqualTo(asList());
store.reset(docId);
assertThat(interceptedCalls.get("reset")).isEqualTo(asList(docId));
store.getOptimizedGraph(docId);
assertThat(interceptedCalls.get("getOptimizedGraph")).isEqualTo(asList(docId));
store.getFullGraph(docId);
assertThat(interceptedCalls.get("getFullGraph")).isEqualTo(asList(docId));
store.getGraphs(asList(docId));
assertThat(interceptedCalls.get("getGraphs")).isEqualTo(asList(asList(docId)));
store.fetchUpdates(docId, revision);
assertThat(interceptedCalls.get("fetchUpdates")).isEqualTo(asList(docId, revision));
store.prune(docId, keep);
assertThat(interceptedCalls.get("prune")).isEqualTo(asList(docId, keep));
store.optimize(docId, keep);
assertThat(interceptedCalls.get("optimize")).isEqualTo(asList(docId, keep));
store.updateBatch(docId);
assertThat(interceptedCalls.get("updateBatch")).isEqualTo(asList(docId));
store.updateBatch(asList(docId));
assertThat(interceptedCalls.get("updateBatch")).isEqualTo(asList(asList(docId)));
}