Allow for shared Objenesis caching in ObjenesisCglibAopProxy
Issue: SPR-12755
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user