Skip to content

Commit

Permalink
Create enhanced FactoryBean instance before applying method intercept…
Browse files Browse the repository at this point in the history
…or (preferably via Objenesis)

Also removes "throws Exception" declaration from ThreadPoolExecutorFactoryBean's getObject method.

Issue: SPR-13095
  • Loading branch information
jhoeller committed Jun 4, 2015
1 parent d195ad2 commit 869b0bc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
Expand Up @@ -39,12 +39,17 @@
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.cglib.proxy.NoOp;
import org.springframework.cglib.transform.ClassEmitterTransformer;
import org.springframework.cglib.transform.TransformingClassGenerator;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisException;
import org.springframework.objenesis.ObjenesisStd;
import org.springframework.objenesis.SpringObjenesis;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -82,6 +87,10 @@ class ConfigurationClassEnhancer {

private static final Log logger = LogFactory.getLog(ConfigurationClassEnhancer.class);

private static final Objenesis cachedObjenesis = new SpringObjenesis();

private static final Objenesis nonCachedObjenesis = new ObjenesisStd(false);


/**
* Loads the specified class and generates a CGLIB subclass of it equipped with
Expand Down Expand Up @@ -388,9 +397,25 @@ private Object enhanceFactoryBean(final Object factoryBean, final ConfigurableBe

Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(factoryBean.getClass());
enhancer.setUseFactory(false);
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallback(new MethodInterceptor() {
enhancer.setCallbackType(MethodInterceptor.class);

// Ideally create enhanced FactoryBean proxy without constructor side effects,
// analogous to AOP proxy creation in ObjenesisCglibAopProxy...
Class<?> fbClass = enhancer.createClass();
Objenesis objenesis = (enhancer.getUseCache() ? cachedObjenesis : nonCachedObjenesis);
Factory factory;
try {
factory = (Factory) objenesis.newInstance(fbClass);
}
catch (ObjenesisException ex) {
// Fallback to regular proxy construction on unsupported JVMs
logger.debug("Unable to instantiate enhanced FactoryBean using Objenesis, " +
"falling back to regular construction", ex);
factory = (Factory) fbClass.newInstance();
}

factory.setCallback(0, new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getObject") && args.length == 0) {
Expand All @@ -399,7 +424,8 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
return proxy.invoke(factoryBean, args);
}
});
return enhancer.create();

return factory;
}

private ConfigurableBeanFactory getBeanFactory(Object enhancedConfigInstance) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -187,7 +187,7 @@ protected BlockingQueue<Runnable> createQueue(int queueCapacity) {


@Override
public ExecutorService getObject() throws Exception {
public ExecutorService getObject() {
return this.exposedExecutor;
}

Expand Down
@@ -0,0 +1,67 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.springframework.scheduling.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;

import org.junit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.junit.Assert.*;

/**
* @author Juergen Hoeller
*/
public class ThreadPoolExecutorFactoryBeanTests {

@Test
public void defaultExecutor() throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(ExecutorConfig.class);
ExecutorService executor = context.getBean("executor", ExecutorService.class);

FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
return "foo";
}
});
executor.execute(task);
assertEquals("foo", task.get());
}


@Configuration
public static class ExecutorConfig {

@Bean
public ThreadPoolExecutorFactoryBean executorFactory() {
return new ThreadPoolExecutorFactoryBean();
}

@Bean
public ExecutorService executor() {
return executorFactory().getObject();
}
}

}

0 comments on commit 869b0bc

Please sign in to comment.