Allow for shared Objenesis caching in ObjenesisCglibAopProxy

Issue: SPR-12755
This commit is contained in:
Juergen Hoeller
2015-02-26 18:34:39 +01:00
parent 09fde31732
commit 287045ef74
5 changed files with 90 additions and 14 deletions

View File

@@ -328,6 +328,7 @@ project("spring-core") {
jarjar("com.googlecode.jarjar:jarjar:1.3")
compile(files(cglibRepackJar))
compile(files(objenesisRepackJar))
compile("commons-logging:commons-logging:1.2")
optional("commons-codec:commons-codec:1.10")
optional("org.aspectj:aspectjweaver:${aspectjVersion}")

View File

@@ -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.
@@ -22,14 +22,17 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Factory;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisException;
import org.springframework.objenesis.ObjenesisStd;
import org.springframework.objenesis.SpringObjenesis;
/**
* Objenesis based extension of {@link CglibAopProxy} to create proxy instances without
* invoking the constructor of the class.
* Objenesis-based extension of {@link CglibAopProxy} to create proxy instances
* without invoking the constructor of the class.
*
* @author Oliver Gierke
* @author Juergen Hoeller
* @since 4.0
*/
@SuppressWarnings("serial")
@@ -37,16 +40,17 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
private static final Log logger = LogFactory.getLog(ObjenesisCglibAopProxy.class);
private final ObjenesisStd objenesis;
private static final Objenesis cachedObjenesis = new SpringObjenesis();
private static final Objenesis nonCachedObjenesis = new ObjenesisStd(false);
/**
* Creates a new {@link ObjenesisCglibAopProxy} using the given {@link AdvisedSupport}.
* @param config must not be {@literal null}.
* Create a new ObjenesisCglibAopProxy for the given AOP configuration.
* @param config the AOP configuration as AdvisedSupport object
*/
public ObjenesisCglibAopProxy(AdvisedSupport config) {
super(config);
this.objenesis = new ObjenesisStd(true);
}
@@ -54,15 +58,14 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
@SuppressWarnings("unchecked")
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
try {
Factory factory = (Factory) this.objenesis.newInstance(enhancer.createClass());
Objenesis objenesis = (enhancer.getUseCache() ? cachedObjenesis : nonCachedObjenesis);
Factory factory = (Factory) objenesis.newInstance(enhancer.createClass());
factory.setCallbacks(callbacks);
return factory;
}
catch (ObjenesisException ex) {
// Fallback to regular proxy construction on unsupported JVMs
if (logger.isDebugEnabled()) {
logger.debug("Unable to instantiate proxy using Objenesis, falling back to regular proxy construction", ex);
}
logger.debug("Unable to instantiate proxy using Objenesis, falling back to regular proxy construction", ex);
return super.createProxyClassAndInstance(enhancer, callbacks);
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.objenesis;
import org.springframework.objenesis.instantiator.ObjectInstantiator;
import org.springframework.objenesis.strategy.InstantiatorStrategy;
import org.springframework.objenesis.strategy.StdInstantiatorStrategy;
import org.springframework.util.ConcurrentReferenceHashMap;
/**
* Spring-specific variant of {@link ObjenesisStd} / {@link ObjenesisBase},
* providing a cache based on {@code Class} keys instead of class names.
*
* @author Juergen Hoeller
* @since 4.2
*/
public class SpringObjenesis implements Objenesis {
private final InstantiatorStrategy strategy = new StdInstantiatorStrategy();
private final ConcurrentReferenceHashMap<Class<?>, ObjectInstantiator<?>> cache =
new ConcurrentReferenceHashMap<Class<?>, ObjectInstantiator<?>>();
public <T> T newInstance(Class<T> clazz) {
return getInstantiatorOf(clazz).newInstance();
}
@SuppressWarnings("unchecked")
public <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz) {
ObjectInstantiator<?> instantiator = this.cache.get(clazz);
if (instantiator == null) {
ObjectInstantiator<?> newInstantiator = this.strategy.newInstantiatorOf(clazz);
instantiator = this.cache.putIfAbsent(clazz, newInstantiator);
if (instantiator == null) {
instantiator = newInstantiator;
}
}
return (ObjectInstantiator<T>) instantiator;
}
}

View File

@@ -0,0 +1,15 @@
/**
* Spring's repackaging of
* <a href="http://objenesis.org">Objenesis 2.1</a>
* (for internal use only).
*
* <p>This repackaging technique avoids any potential conflicts with
* dependencies on different Objenesis versions at the application
* level or from third-party libraries and frameworks.
*
* <p>As this repackaging happens at the class file level, sources
* and javadocs are not available here. See the original
* <a href="http://objenesis.org/tutorial.html">Objenesis docs</a>
* for details when working with these classes.
*/
package org.springframework.objenesis;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@@ -40,7 +40,8 @@ import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.objenesis.ObjenesisStd;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.SpringObjenesis;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -79,7 +80,7 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
private static final Log logger = LogFactory.getLog(MvcUriComponentsBuilder.class);
private static final ObjenesisStd objenesis = new ObjenesisStd(true);
private static final Objenesis objenesis = new SpringObjenesis();
private static final PathMatcher pathMatcher = new AntPathMatcher();