Top Description Inners Fields Constructors Methods
org.springframework.beans.factory.support

public Class CglibSubclassingInstantiationStrategy

extends SimpleInstantiationStrategy
Class Inheritance
Imports
java.lang.reflect.Constructor, .Method, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeanInstantiationException, .BeanUtils, org.springframework.beans.factory.BeanFactory, org.springframework.beans.factory.config.ConfigurableBeanFactory, org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy, .SpringNamingPolicy, org.springframework.cglib.proxy.Callback, .CallbackFilter, .Enhancer, .Factory, .MethodInterceptor, .MethodProxy, .NoOp, org.springframework.core.ResolvableType, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

Default object instantiation strategy for use in BeanFactories.

Uses CGLIB to generate subclasses dynamically if methods need to be overridden by the container to implement Method Injection.

Authors
Rod Johnson, Juergen Hoeller, Sam Brannen
Since
1.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CglibSubclassingInstantiationStrategy.CglibIdentitySupport

Class providing hashCode and equals methods required by CGLIB to ensure that CGLIB doesn't generate a distinct class per bean.

private static class
CglibSubclassingInstantiationStrategy.CglibSubclassCreator

An inner class created for historical reasons to avoid external CGLIB dependency in Spring versions earlier than 3.2.

private static class
CglibSubclassingInstantiationStrategy.LookupOverrideMethodInterceptor

CGLIB MethodInterceptor to override methods, replacing them with an implementation that returns a bean looked up in the container.

private static class
CglibSubclassingInstantiationStrategy.MethodOverrideCallbackFilter

CGLIB callback for filtering method interception behavior.

private static class
CglibSubclassingInstantiationStrategy.ReplaceOverrideMethodInterceptor

CGLIB MethodInterceptor to override methods, replacing them with a call to a generic MethodReplacer.

Field Summary

Modifier and TypeField and Description
private static final int
LOOKUP_OVERRIDE

Index in the CGLIB callback array for a method that should be overridden to provide method lookup.

private static final int
METHOD_REPLACER

Index in the CGLIB callback array for a method that should be overridden using generic method replacer functionality.

private static final int
PASSTHROUGH

Index in the CGLIB callback array for passthrough behavior, in which case the subclass won't override the original class.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Class<?>
getActualBeanClass(RootBeanDefinition bd, String beanName, BeanFactory owner)

Overrides default org.springframework.beans.factory.support.InstantiationStrategy.getActualBeanClass.

Determine the actual class for the given bean definition, as instantiated at runtime.

protected Object
instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner)

Overrides org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithMethodInjection.

Subclasses can override this method, which is implemented to throw UnsupportedOperationException, if they can instantiate an object with the Method Injection specified in the given RootBeanDefinition.

protected Object
instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner, Constructor<?> ctor, Object... args)

Overrides org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithMethodInjection.

Subclasses can override this method, which is implemented to throw UnsupportedOperationException, if they can instantiate an object with the Method Injection specified in the given RootBeanDefinition.

Inherited from org.springframework.beans.factory.support.SimpleInstantiationStrategy:
getCurrentlyInvokedFactoryMethodinstantiateinstantiateinstantiatesetCurrentlyInvokedFactoryMethod

Field Detail

LOOKUP_OVERRIDEback to summary
private static final int LOOKUP_OVERRIDE

Index in the CGLIB callback array for a method that should be overridden to provide method lookup.

METHOD_REPLACERback to summary
private static final int METHOD_REPLACER

Index in the CGLIB callback array for a method that should be overridden using generic method replacer functionality.

PASSTHROUGHback to summary
private static final int PASSTHROUGH

Index in the CGLIB callback array for passthrough behavior, in which case the subclass won't override the original class.

Constructor Detail

CglibSubclassingInstantiationStrategyback to summary
public CglibSubclassingInstantiationStrategy()

Method Detail

getActualBeanClassback to summary
public Class<?> getActualBeanClass(RootBeanDefinition bd, String beanName, BeanFactory owner)

Overrides default org.springframework.beans.factory.support.InstantiationStrategy.getActualBeanClass.

Doc from org.springframework.beans.factory.support.InstantiationStrategy.getActualBeanClass.

Determine the actual class for the given bean definition, as instantiated at runtime.

Annotations
@Override
instantiateWithMethodInjectionback to summary
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner)

Overrides org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithMethodInjection.

Doc from org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithMethodInjection.

Subclasses can override this method, which is implemented to throw UnsupportedOperationException, if they can instantiate an object with the Method Injection specified in the given RootBeanDefinition. Instantiation should use a no-arg constructor.

Annotations
@Override
instantiateWithMethodInjectionback to summary
protected Object instantiateWithMethodInjection(RootBeanDefinition bd, String beanName, BeanFactory owner, Constructor<?> ctor, Object... args)

Overrides org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithMethodInjection.

Doc from org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithMethodInjection.

Subclasses can override this method, which is implemented to throw UnsupportedOperationException, if they can instantiate an object with the Method Injection specified in the given RootBeanDefinition. Instantiation should use the given constructor and parameters.

Annotations
@Override
org.springframework.beans.factory.support back to summary

private Class CglibSubclassingInstantiationStrategy.CglibIdentitySupport

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibIdentitySupport
Known Direct Subclasses
org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.MethodOverrideCallbackFilter, org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.LookupOverrideMethodInterceptor, org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.ReplaceOverrideMethodInterceptor

Class providing hashCode and equals methods required by CGLIB to ensure that CGLIB doesn't generate a distinct class per bean. Identity is based on class and bean definition.

Field Summary

Modifier and TypeField and Description
private final RootBeanDefinition

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public RootBeanDefinition
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

beanDefinitionback to summary
private final RootBeanDefinition beanDefinition

Constructor Detail

CglibIdentitySupportback to summary
public CglibIdentitySupport(RootBeanDefinition beanDefinition)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getBeanDefinitionback to summary
public RootBeanDefinition getBeanDefinition()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
org.springframework.beans.factory.support back to summary

private Class CglibSubclassingInstantiationStrategy.CglibSubclassCreator

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibSubclassCreator

An inner class created for historical reasons to avoid external CGLIB dependency in Spring versions earlier than 3.2.

Field Summary

Modifier and TypeField and Description
private final RootBeanDefinition
private static final Class<?>[]
private final BeanFactory

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Class<?>
createEnhancedSubclass(RootBeanDefinition beanDefinition)

Create an enhanced subclass of the bean class for the provided bean definition, using CGLIB.

public Object

Returns:

new instance of the dynamically generated subclass
instantiate
(Constructor<?>
constructor to use. If this is null, use the no-arg constructor (no parameterization, or Setter Injection)
ctor
,
Object...
arguments to use for the constructor. Ignored if the ctor parameter is null.
args
)

Create a new instance of a dynamically generated subclass implementing the required lookups.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanDefinitionback to summary
private final RootBeanDefinition beanDefinition
CALLBACK_TYPESback to summary
private static final Class<?>[] CALLBACK_TYPES
ownerback to summary
private final BeanFactory owner

Constructor Detail

CglibSubclassCreatorback to summary
pack-priv CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner)

Method Detail

createEnhancedSubclassback to summary
public Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition)

Create an enhanced subclass of the bean class for the provided bean definition, using CGLIB.

instantiateback to summary
public Object instantiate(Constructor<?> ctor, Object... args)

Create a new instance of a dynamically generated subclass implementing the required lookups.

Parameters
ctor:Constructor<?>

constructor to use. If this is null, use the no-arg constructor (no parameterization, or Setter Injection)

args:Object[]

arguments to use for the constructor. Ignored if the ctor parameter is null.

Returns:Object

new instance of the dynamically generated subclass

org.springframework.beans.factory.support back to summary

private Class CglibSubclassingInstantiationStrategy.LookupOverrideMethodInterceptor

extends CglibIdentitySupport
implements MethodInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.cglib.proxy.MethodInterceptor, org.springframework.cglib.proxy.Callback

CGLIB MethodInterceptor to override methods, replacing them with an implementation that returns a bean looked up in the container.

Field Summary

Modifier and TypeField and Description
private final BeanFactory

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
intercept(Object
"this", the enhanced object
obj
,
Method
intercepted Method
method
,
Object[]
argument array; primitive types are wrapped
args
,
MethodProxy
used to invoke super (non-intercepted method); may be called as many times as needed
mp
)

Implements org.springframework.cglib.proxy.MethodInterceptor.intercept.

All generated proxied methods call this method instead of the original method.

Inherited from org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibIdentitySupport:
equalsgetBeanDefinitionhashCode

Field Detail

ownerback to summary
private final BeanFactory owner

Constructor Detail

LookupOverrideMethodInterceptorback to summary
public LookupOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner)

Method Detail

interceptback to summary
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable

Implements org.springframework.cglib.proxy.MethodInterceptor.intercept.

Doc from org.springframework.cglib.proxy.MethodInterceptor.intercept.

All generated proxied methods call this method instead of the original method. The original method may either be invoked by normal reflection using the Method object, or by using the MethodProxy (faster).

Parameters
obj:Object

"this", the enhanced object

method:Method

intercepted Method

args:Object[]

argument array; primitive types are wrapped

mp:MethodProxy

used to invoke super (non-intercepted method); may be called as many times as needed

Returns:Object

any value compatible with the signature of the proxied method. Method returning void will ignore this value.

Annotations
@Override
Exceptions
Throwable:
any exception may be thrown; if so, super method will not be invoked
org.springframework.beans.factory.support back to summary

private Class CglibSubclassingInstantiationStrategy.MethodOverrideCallbackFilter

extends CglibIdentitySupport
implements CallbackFilter
Class Inheritance
All Implemented Interfaces
org.springframework.cglib.proxy.CallbackFilter

CGLIB callback for filtering method interception behavior.

Field Summary

Modifier and TypeField and Description
private static final Log

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int
accept(Method
the intercepted method
method
)

Implements org.springframework.cglib.proxy.CallbackFilter.accept.

Map a method to a callback.

Inherited from org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibIdentitySupport:
equalsgetBeanDefinitionhashCode

Field Detail

loggerback to summary
private static final Log logger

Constructor Detail

MethodOverrideCallbackFilterback to summary
public MethodOverrideCallbackFilter(RootBeanDefinition beanDefinition)

Method Detail

acceptback to summary
public int accept(Method method)

Implements org.springframework.cglib.proxy.CallbackFilter.accept.

Doc from org.springframework.cglib.proxy.CallbackFilter.accept.

Map a method to a callback.

Parameters
method:Method

the intercepted method

Returns:int

the index into the array of callbacks (as specified by Enhancer#setCallbacks) to use for the method,

Annotations
@Override
org.springframework.beans.factory.support back to summary

private Class CglibSubclassingInstantiationStrategy.ReplaceOverrideMethodInterceptor

extends CglibIdentitySupport
implements MethodInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.cglib.proxy.MethodInterceptor, org.springframework.cglib.proxy.Callback

CGLIB MethodInterceptor to override methods, replacing them with a call to a generic MethodReplacer.

Field Summary

Modifier and TypeField and Description
private final BeanFactory

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
intercept(Object
"this", the enhanced object
obj
,
Method
intercepted Method
method
,
Object[]
argument array; primitive types are wrapped
args
,
MethodProxy
used to invoke super (non-intercepted method); may be called as many times as needed
mp
)

Implements org.springframework.cglib.proxy.MethodInterceptor.intercept.

All generated proxied methods call this method instead of the original method.

Inherited from org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.CglibIdentitySupport:
equalsgetBeanDefinitionhashCode

Field Detail

ownerback to summary
private final BeanFactory owner

Constructor Detail

ReplaceOverrideMethodInterceptorback to summary
public ReplaceOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner)

Method Detail

interceptback to summary
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable

Implements org.springframework.cglib.proxy.MethodInterceptor.intercept.

Doc from org.springframework.cglib.proxy.MethodInterceptor.intercept.

All generated proxied methods call this method instead of the original method. The original method may either be invoked by normal reflection using the Method object, or by using the MethodProxy (faster).

Parameters
obj:Object

"this", the enhanced object

method:Method

intercepted Method

args:Object[]

argument array; primitive types are wrapped

mp:MethodProxy

used to invoke super (non-intercepted method); may be called as many times as needed

Returns:Object

any value compatible with the signature of the proxied method. Method returning void will ignore this value.

Annotations
@Override
Exceptions
Throwable:
any exception may be thrown; if so, super method will not be invoked