bean.xml文件添加lookup-method和replaced-method标签
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="apple" class="com.mashibing.methodOverrides.lookup.Apple" ><property name="banana" ref="banana"></property></bean><bean id="banana" class="com.mashibing.methodOverrides.lookup.Banana" scope="prototype"></bean><bean id="fruitplate1" class="com.mashibing.methodOverrides.lookup.FruitPlate"><lookup-method name="getFruit" bean="apple"></lookup-method></bean><bean id="fruitplate2" class="com.mashibing.methodOverrides.lookup.FruitPlate"><lookup-method name="getFruit" bean="banana"></lookup-method></bean>
</beans>
lookup-method和replaced-method标签解析方法
parseBeanDefinitionElement:537, BeanDefinitionParserDelegate (org.springframework.beans.factory.xml)
parseBeanDefinitionElement:445, BeanDefinitionParserDelegate (org.springframework.beans.factory.xml)
parseBeanDefinitionElement:410, BeanDefinitionParserDelegate (org.springframework.beans.factory.xml)
processBeanDefinition:326, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
parseDefaultElement:199, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
parseBeanDefinitions:178, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
doRegisterBeanDefinitions:150, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
registerBeanDefinitions:97, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
registerBeanDefinitions:526, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)
doLoadBeanDefinitions:397, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)
loadBeanDefinitions:341, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)
loadBeanDefinitions:310, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)
loadBeanDefinitions:190, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)
loadBeanDefinitions:234, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)
loadBeanDefinitions:197, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)
loadBeanDefinitions:267, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)
loadBeanDefinitions:150, AbstractXmlApplicationContext (org.springframework.context.support)
loadBeanDefinitions:102, AbstractXmlApplicationContext (org.springframework.context.support)
refreshBeanFactory:138, AbstractRefreshableApplicationContext (org.springframework.context.support)
obtainFreshBeanFactory:718, AbstractApplicationContext (org.springframework.context.support)
refresh:577, AbstractApplicationContext (org.springframework.context.support)
<init>:150, ClassPathXmlApplicationContext (org.springframework.context.support)
<init>:87, ClassPathXmlApplicationContext (org.springframework.context.support)
main:19, TestMethodOverride (com.mashibing)
解析之后在befactory的beanMap里存储有:
项目启动,执行创建
createBean:608, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:417, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 1699679644 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$10)
getSingleton:370, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:414, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:260, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:993, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:1024, AbstractApplicationContext (org.springframework.context.support)
refresh:614, AbstractApplicationContext (org.springframework.context.support)
<init>:150, ClassPathXmlApplicationContext (org.springframework.context.support)
<init>:87, ClassPathXmlApplicationContext (org.springframework.context.support)
main:19, TestMethodOverride (com.mashibing)
/*** Validate and prepare the method overrides defined for this bean.* Checks for existence of a method with the specified name.* @throws BeanDefinitionValidationException in case of validation failure*/public void prepareMethodOverrides() throws BeanDefinitionValidationException {// Check that lookup methods exist and determine their overloaded status.if (hasMethodOverrides()) {getMethodOverrides().getOverrides().forEach(this::prepareMethodOverride);}}
instantiateBean:1605, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBeanInstance:1495, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:675, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:630, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:417, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 1699679644 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$10)
getSingleton:370, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:414, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:260, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:993, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:1024, AbstractApplicationContext (org.springframework.context.support)
refresh:614, AbstractApplicationContext (org.springframework.context.support)
<init>:150, ClassPathXmlApplicationContext (org.springframework.context.support)
<init>:87, ClassPathXmlApplicationContext (org.springframework.context.support)
main:19, TestMethodOverride (com.mashibing)
beanFactory中的对象默认实例化策略 CglibSubclassingInstantiationStrategy
InstantiationStrategy源码
/*** 实例化策略接口,子类被用来根据rootBeanDefinition来创建实例对象* 这是一个策略,因为各种方法是可能的,包括使用CGLIB动态支持创建子类方法注入** @author Rod Johnson* @author Juergen Hoeller* @since 1.1*/
public interface InstantiationStrategy {/*** 使用默认构造方法进行实例化*/Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner)throws BeansException;/*** 通过指定构造器来进行实例化*/Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,Constructor<?> ctor, Object... args) throws BeansException;/*** 通过指定工厂方法来进行实例化*/Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,@Nullable Object factoryBean, Method factoryMethod, Object... args)throws BeansException;}
SimpleInstantiationStrategy 源码
package org.springframework.beans.factory.support;/*** 简单的对象实例化策略*/
public class SimpleInstantiationStrategy implements InstantiationStrategy {// FactoryMethod的ThreadLocal对象private static final ThreadLocal<Method> currentlyInvokedFactoryMethod = new ThreadLocal<>();/*** 返回当前现成所有的FactoryMethod变量值@Nullablepublic static Method getCurrentlyInvokedFactoryMethod() {return currentlyInvokedFactoryMethod.get();}/*** 如果发现是有方法重载的,就需要用cglib来动态代理,如果没有就直接获取默认构造方法实例化*/@Overridepublic Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {// Don't override the class with CGLIB if no overrides.// bd对象定义中,是否包含MethodOverride列表,spring中有两个标签参数会产生MethodOverrides,分别是lookup-method,replaced-method// 没有MethodOverrides对象,可以直接实例化if (!bd.hasMethodOverrides()) {// 实例化对象的构造方法Constructor<?> constructorToUse;// 锁定对象,使获得实例化构造方法线程安全synchronized (bd.constructorArgumentLock) {// 查看bd对象里使用否含有构造方法constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;// 如果没有if (constructorToUse == null) {// 从bd中获取beanClassfinal Class<?> clazz = bd.getBeanClass();// 如果要实例化的beanDefinition是一个接口,则直接抛出异常if (clazz.isInterface()) {throw new BeanInstantiationException(clazz, "Specified class is an interface");}try {// 获取系统安全管理器if (System.getSecurityManager() != null) {constructorToUse = AccessController.doPrivileged((PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);}else {// 获取默认的无参构造器constructorToUse = clazz.getDeclaredConstructor();}// 获取到构造器之后将构造器赋值给bd中的属性bd.resolvedConstructorOrFactoryMethod = constructorToUse;}catch (Throwable ex) {throw new BeanInstantiationException(clazz, "No default constructor found", ex);}}}// 通过反射生成具体的实例化对象return BeanUtils.instantiateClass(constructorToUse);}else {// Must generate CGLIB subclass.// 必须生成cglib子类return instantiateWithMethodInjection(bd, beanName, owner);}}/*** 此方法没有做任何的实现,直接抛出异常,可以由子类重写覆盖*/protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");}/*** 使用有参构造方法进行实例化*/@Overridepublic Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,final Constructor<?> ctor, Object... args) {// 检查bd对象是否有MethodOverrides对象,没有的话则直接实例化对象if (!bd.hasMethodOverrides()) {if (System.getSecurityManager() != null) {// use own privileged to change accessibility (when security is on)AccessController.doPrivileged((PrivilegedAction<Object>) () -> {ReflectionUtils.makeAccessible(ctor);return null;});}// 通过反射实例化对象return BeanUtils.instantiateClass(ctor, args);}else {// 如果有methodOverride对象,则调用方法来进行实现return instantiateWithMethodInjection(bd, beanName, owner, ctor, args);}}/*** 此方法没有做任何的实现,直接抛出异常,可以由子类重写覆盖*protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName,BeanFactory owner, @Nullable Constructor<?> ctor, Object... args) {throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");}/*** 使用工厂方法进行实例化*/@Overridepublic Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,@Nullable Object factoryBean, final Method factoryMethod, Object... args) {try {// 是否包含系统安全管理器if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {ReflectionUtils.makeAccessible(factoryMethod);return null;});}else {// 通过反射工具类设置访问权限ReflectionUtils.makeAccessible(factoryMethod);}// 获取原有的Method对象Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get();try {// 设置当前的MethodcurrentlyInvokedFactoryMethod.set(factoryMethod);// 使用factoryMethod实例化对象Object result = factoryMethod.invoke(factoryBean, args);if (result == null) {result = new NullBean();}return result;}finally {// 实例化完成后回复现场if (priorInvokedFactoryMethod != null) {currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod);}else {currentlyInvokedFactoryMethod.remove();}}}catch (IllegalArgumentException ex) {throw new BeanInstantiationException(factoryMethod,"Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +"args: " + StringUtils.arrayToCommaDelimitedString(args), ex);}catch (IllegalAccessException ex) {throw new BeanInstantiationException(factoryMethod,"Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);}catch (InvocationTargetException ex) {String msg = "Factory method '" + factoryMethod.getName() + "' threw exception";if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory &&((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) {msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " +"declaring the factory method as static for independence from its containing instance. " + msg;}throw new BeanInstantiationException(factoryMethod, msg, ex.getTargetException());}}}
CglibSubclassingInstantiationStrategy源码
package org.springframework.beans.factory.support;
/*** beanFactory中的对象默认实例化策略*/
public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationStrategy {/*** 如果没有override方法覆盖的话,那么索引位置为0* /private static final int PASSTHROUGH = 0;/*** 如果有lookup-method的覆盖,那么索引位置为1*/private static final int LOOKUP_OVERRIDE = 1;/*** 如果有replace-method的覆盖,那么索引位置为2*/private static final int METHOD_REPLACER = 2;// 子类重写SimpleInstantiationStrategy中的instantiateWithMethodInjection方法@Overrideprotected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {return instantiateWithMethodInjection(bd, beanName, owner, null);}@Overrideprotected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,@Nullable Constructor<?> ctor, Object... args) {// Must generate CGLIB subclass...// 必须生成一个cglib的子类return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);}private static class CglibSubclassCreator {// 此处定义了一个回调类型的数组,后面MethodOverrideCallbackFilter通过指定下标获取对应的拦截private static final Class<?>[] CALLBACK_TYPES = new Class<?>[]{NoOp.class, LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class};private final RootBeanDefinition beanDefinition;private final BeanFactory owner;// 构造方法CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner) {this.beanDefinition = beanDefinition;this.owner = owner;}/*** 实例化入口:动态构造子类*/public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {// 根据beanDefinition来创建一个cglib的子类Class<?> subclass = createEnhancedSubclass(this.beanDefinition);Object instance;// 如果构造器等于空,那么直接通过反射来实例化对象if (ctor == null) {instance = BeanUtils.instantiateClass(subclass);}else {try {// 通过cglib对象来根据参数类型获取对应的构造器Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());// 通过构造器来获取对象instance = enhancedSubclassConstructor.newInstance(args);}catch (Exception ex) {throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);}}// SPR-10785: set callbacks directly on the instance instead of in the// enhanced class (via the Enhancer) in order to avoid memory leaks.// https://jira.spring.io/browse/SPR-10785Factory factory = (Factory) instance;factory.setCallbacks(new Callback[] {NoOp.INSTANCE,new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});return instance;}private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {// cglib规定用法,对原始class进行增强,并设置callbackEnhancer enhancer = new Enhancer();enhancer.setSuperclass(beanDefinition.getBeanClass());enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);if (this.owner instanceof ConfigurableBeanFactory) {ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));}// 过滤,自定义逻辑来指定调用的callback下标enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));// 只是产生class就直接指定callback类型,跟上面指定的callbackFilter对应enhancer.setCallbackTypes(CALLBACK_TYPES);return enhancer.createClass();}}private static class CglibIdentitySupport {private final RootBeanDefinition beanDefinition;public CglibIdentitySupport(RootBeanDefinition beanDefinition) {this.beanDefinition = beanDefinition;}public RootBeanDefinition getBeanDefinition() {return this.beanDefinition;}@Overridepublic boolean equals(@Nullable Object other) {return (other != null && getClass() == other.getClass() &&this.beanDefinition.equals(((CglibIdentitySupport) other).beanDefinition));}@Overridepublic int hashCode() {return this.beanDefinition.hashCode();}}/*** CGLIB callback for filtering method interception behavior.*/private static class MethodOverrideCallbackFilter extends CglibIdentitySupport implements CallbackFilter {private static final Log logger = LogFactory.getLog(MethodOverrideCallbackFilter.class);public MethodOverrideCallbackFilter(RootBeanDefinition beanDefinition) {super(beanDefinition);}@Overridepublic int accept(Method method) {// 根据beanDefinition的相关配置加载到MEthodOverrides集合中MethodOverride methodOverride = getBeanDefinition().getMethodOverrides().getOverride(method);if (logger.isTraceEnabled()) {logger.trace("MethodOverride for " + method + ": " + methodOverride);}if (methodOverride == null) {return PASSTHROUGH;}else if (methodOverride instanceof LookupOverride) {return LOOKUP_OVERRIDE;}else if (methodOverride instanceof ReplaceOverride) {return METHOD_REPLACER;}throw new UnsupportedOperationException("Unexpected MethodOverride subclass: " +methodOverride.getClass().getName());}}/*** CGLIB MethodInterceptor to override methods, replacing them with an* implementation that returns a bean looked up in the container.*/private static class LookupOverrideMethodInterceptor extends CglibIdentitySupport implements MethodInterceptor {private final BeanFactory owner;public LookupOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner) {super(beanDefinition);this.owner = owner;}@Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {// Cast is safe, as CallbackFilter filters are used selectively.LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);Assert.state(lo != null, "LookupOverride not found");Object[] argsToUse = (args.length > 0 ? args : null); // if no-arg, don't insist on args at allif (StringUtils.hasText(lo.getBeanName())) {return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :this.owner.getBean(lo.getBeanName()));}else {return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :this.owner.getBean(method.getReturnType()));}}}/*** CGLIB MethodInterceptor to override methods, replacing them with a call* to a generic MethodReplacer.*/private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySupport implements MethodInterceptor {private final BeanFactory owner;public ReplaceOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner) {super(beanDefinition);this.owner = owner;}@Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {ReplaceOverride ro = (ReplaceOverride) getBeanDefinition().getMethodOverrides().getOverride(method);Assert.state(ro != null, "ReplaceOverride not found");// TODO could cache if a singleton for minor performance optimizationMethodReplacer mr = this.owner.getBean(ro.getMethodReplacerBeanName(), MethodReplacer.class);return mr.reimplement(obj, method, args);}}}
instantiate:131, CglibSubclassingInstantiationStrategy$CglibSubclassCreator (org.springframework.beans.factory.support)
instantiateWithMethodInjection:94, CglibSubclassingInstantiationStrategy (org.springframework.beans.factory.support)
instantiateWithMethodInjection:85, CglibSubclassingInstantiationStrategy (org.springframework.beans.factory.support)
instantiate:119, SimpleInstantiationStrategy (org.springframework.beans.factory.support)
instantiateBean:1605, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBeanInstance:1495, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:675, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:630, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:417, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 1699679644 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$10)
getSingleton:370, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:414, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:260, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:993, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:1024, AbstractApplicationContext (org.springframework.context.support)
refresh:614, AbstractApplicationContext (org.springframework.context.support)
<init>:150, ClassPathXmlApplicationContext (org.springframework.context.support)
<init>:87, ClassPathXmlApplicationContext (org.springframework.context.support)
main:19, TestMethodOverride (com.mashibing)
如果没有调用lookup-method和replaced-method,会调用SimpleInstantiationStrategy#
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner)
// 通过反射生成具体的实例化对象
return BeanUtils.instantiateClass(constructorToUse);
有这两个标签,会CglibSubclassingInstantiationStrategy#
public Object instantiate(@Nullable Constructor<?> ctor, Object… args)方法
/*** 实例化入口:动态构造子类** Create a new instance of a dynamically generated subclass implementing the* required lookups.* @param ctor constructor to use. If this is {@code null}, use the* no-arg constructor (no parameterization, or Setter Injection)* @param args arguments to use for the constructor.* Ignored if the {@code ctor} parameter is {@code null}.* @return new instance of the dynamically generated subclass*/public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {// 根据beanDefinition来创建一个cglib的子类Class<?> subclass = createEnhancedSubclass(this.beanDefinition);Object instance;// 如果构造器等于空,那么直接通过反射来实例化对象if (ctor == null) {instance = BeanUtils.instantiateClass(subclass);}else {try {// 通过cglib对象来根据参数类型获取对应的构造器Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());// 通过构造器来获取对象instance = enhancedSubclassConstructor.newInstance(args);}catch (Exception ex) {throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);}}// SPR-10785: set callbacks directly on the instance instead of in the// enhanced class (via the Enhancer) in order to avoid memory leaks.// https://jira.spring.io/browse/SPR-10785Factory factory = (Factory) instance;factory.setCallbacks(new Callback[] {NoOp.INSTANCE,new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});return instance;}