spring学习(六)——5.2 Bean实例化策略

参考文章:

http://www.iocoder.cn/

 

bean的实例化过程

AbstractAutowireCapableBeanFactory#doCreateBean(...) 方法。在这个方法中有 Bean 的实例化、属性注入和初始化过程。主要是createBeanInstance方法

  • createBeanInstance(...) 方法中,如果 Supplier 不为空,则调用 #obtainFromSupplier(...) 实例化 bean
  • 如果 factory 不为空,则调用 #instantiateUsingFactoryMethod(...) 方法来实例化 Bean
  • 如果都不是,则调用 #instantiateBean(...) 方法来实例化 Bean 

InstantiationStrategy

InstantiationStrategy 接口定义了 Spring Bean 实例化的策略

三种策略:

  • 无参构造方法
  • 有参构造方法
  • 工厂方法
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;

}

InstantiationStrategy的实现类

InstantiationStrategy 接口有两个实现类:SimpleInstantiationStrategy 和 CglibSubclassingInstantiationStrategy

SimpleInstantiationStrategy

默认的构造方法

	@Override
	public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
		// Don't override the class with CGLIB if no overrides.
		// 没有覆盖,直接使用反射实例化即可
		if (!bd.hasMethodOverrides()) {
			// 获得构造方法 constructorToUse
			Constructor<?> constructorToUse;
			synchronized (bd.constructorArgumentLock) {
				constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
				if (constructorToUse == null) {
					final Class<?> clazz = bd.getBeanClass();
					// 如果是接口,抛出 BeanInstantiationException 异常
					if (clazz.isInterface()) {
						throw new BeanInstantiationException(clazz, "Specified class is an interface");
					}
					try {
						// 从 clazz 中,获得构造方法
						if (System.getSecurityManager() != null) {
							constructorToUse = AccessController.doPrivileged(
									(PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
						}
						else {
							constructorToUse = clazz.getDeclaredConstructor();
						}
						// 标记 resolvedConstructorOrFactoryMethod 属性
						bd.resolvedConstructorOrFactoryMethod = constructorToUse;
					}
					catch (Throwable ex) {
						throw new BeanInstantiationException(clazz, "No default constructor found", ex);
					}
				}
			}
			// 通过 BeanUtils 直接使用构造器对象实例化 Bean 对象
			return BeanUtils.instantiateClass(constructorToUse);
		}
		else {
			// Must generate CGLIB subclass.
			// 生成 CGLIB 创建的子类对象
			return instantiateWithMethodInjection(bd, beanName, owner);
		}
	}

判断bd.hasMethodOverrides()如果没有者直接使用反射,否则需要CGLIB 实例化对象来实例化bean

工厂方法

@Override
	public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
			@Nullable Object factoryBean, final Method factoryMethod, Object... args) {

		try {
			// 设置 Method 可访问
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
					ReflectionUtils.makeAccessible(factoryMethod);
					return null;
				});
			}
			else {
				ReflectionUtils.makeAccessible(factoryMethod);
			}
			// 获得原 Method 对象
			Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get();
			try {
				// 设置新的 Method 对象,到 currentlyInvokedFactoryMethod 中
				currentlyInvokedFactoryMethod.set(factoryMethod);
				// 创建bean
				Object result = factoryMethod.invoke(factoryBean, args);
				// bean不存在则创建nullbean
				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

 CglibSubclassingInstantiationStrategy 为 Spring 实例化 Bean 的默认实例化策略,其主要功能还是对父类功能进行补充

@Override
	protected 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);
	}

CglibSubclassingInstantiationStrategy 实例化 Bean 策略,是通过其内部类 CglibSubclassCreator 来实现的

public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
			// 通过 Cglib 创建一个代理类
			Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
			Object instance;
			// 没有构造器,通过 BeanUtils 使用默认构造器创建一个bean实例
			if (ctor == null) {
				// 实例化子类
				instance = BeanUtils.instantiateClass(subclass);
			}
			else {
				try {
					// 获取代理类对应的构造器对象,并实例化 bean
					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.
			Factory factory = (Factory) instance;
			// 为了避免 memory leaks 异常,直接在 bean 实例上设置回调对象
			factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
					new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
					new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
			return instance;
		}

createEnhancedSubclass-bean的增强子类

private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
			// 创建Enhancer对象
			Enhancer enhancer = new Enhancer();
			// 设置bean类
			enhancer.setSuperclass(beanDefinition.getBeanClass());
			// 设置命名方式
			enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
			// 设置生产策略
			if (this.owner instanceof ConfigurableBeanFactory) {
				ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
				enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
			}
			// 设置回调过滤
			enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
			enhancer.setCallbackTypes(CALLBACK_TYPES);
			return enhancer.createClass();
		}

      

回调过滤

enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));

CallbackFilter 是 CGLIB 的一个回调过滤器,MethodOverrideCallbackFilter 实现 CallbackFilter 的 #accept(Method method) 方法

private static class MethodOverrideCallbackFilter extends CglibIdentitySupport implements CallbackFilter {

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

		public MethodOverrideCallbackFilter(RootBeanDefinition beanDefinition) {
			super(beanDefinition);
		}

		@Override
		public int accept(Method method) {
			MethodOverride methodOverride = getBeanDefinition().getMethodOverrides().getOverride(method);
			if (logger.isTraceEnabled()) {
				logger.trace("Override for '" + method.getName() + "' is [" + 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());
		}
	}

回调类别

     enhancer.setCallbackTypes(CALLBACK_TYPES);   

对应着三种类的数组

		private static final Class<?>[] CALLBACK_TYPES = new Class<?>[]
				{NoOp.class, LookupOverrideMethodInterceptor.class, ReplaceOverrideMethodInterceptor.class};

其中有了两个拦截器。来实现方法替换

  • LookupOverrideMethodInterceptor 
  • ReplaceOverrideMethodInterceptor

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1 Spring基本特征 6 2 Spring的组成 6 2.1 Spring的jar包 6 2.2 Spring配置文件 7 2.3 Spring API 8 3 Spring基本功能详解 8 3.1 SpringIOC 8 3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化(简单工厂模式) 14 3.3.3初始化(创建)bean时机 15 Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定Bean的初始化方法和销毁方法 18 Bean的初始化和销毁拓展: 18 Spring的IOC总结: 20 3.5 依赖注入(DI) 20 3.5.1 使用构造器注入 20 3.5.2 使用属性setting方法进行注入 21 3.5.3 装配list集合 22 3.5.4 装配set集合 22 3.5.5 装配map 22 3.5.6 装配Properties 23 3.6 注解注入 23 注解注入拓展: 23 3.6.1 @Autowired 26 3.6.2 @Qualifier 27 3.6.3 @Resource 27 3.6.4 @PostConstruct 28 3.6.5 @PreDestroy 28 注解注入拓展: 28 3.7扫描注入 30 注解扫描拓展: 32 Mvc用注解写: 34 Spring容器IOC和di的整个启动过程: 38 3.8 spring中的继承 38 拓展spring为类中的属性赋值: 40 小结: 47 面向接口编程: 47 4 面向切面编程 52 4.1 代理模式 52 代理模式拓展: 52 4.1.1 JDK动态代理 58 JDK动态代理拓展: 59 4.1.2 CGLIB做代理 66 CGLIB动态代理拓展: 68 4.1.3 Spring的动态代理 71 4.2 AOP编程 71 4.2.1概念: 71 SpringAOP概念拓展: 73 之前实现了目标方法的动态调用,现在来实现切面的动态调用。 74 4.2.2 AOP实现的两种模式 78 4.2.2.1 xml形式 78 XML形式拓展: 81 异常通知处理例子: 91 不用spring异常通知,另一种处理异常 96 4.2.2.2Aop注解形式(了解) 99 注解注入拓展: 103 5 Spring数据库 106 5.1 Spring+JDBC 106 5.1.1 Jdbc编程特点 106 5.1.2引入DataSource 106 5.1.3 核心类JdbcTemplate 106 5.1.4 使用JdbcTemplate 106 5.1.5 继承JdbcDaoSupport 107 5.1.6 使用properties文件 107 5.1.7 RowMapper的使用 107 拓展: 108 DataSource注入的三种方式: 108 5.1.8声明式事务管理 116 5.1.8.1Spring的事务管理器 117 5.1.8.2Spring事务的传播属性 117 5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 5.1.8.5以注解方式配置 125 拓展: 127 5.1.9使用CGLIB以XML形式配置事务 130 5.2 Spring+Hibernate 131 5.2.1 HibernateTemplate模板 131 5.2.2 声明式事务 131 配置XML文件 131 拓展: 132 注解形式: 137 拓展: 138 6 Struts2+spring+hibernate 141 6.1 需要添加的jar包 141 6.2 Spring融合web服务器 141 6.3 struts.xml文件 143 6.4 OpenInSessionView 143 拓展: 144 实例: 146
1. spring 1.1 【第二章】 IoC 之 2.3 IoC的配置使用——跟我学Spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2 【第二章】 IoC 之 2.1 IoC基础 ——跟我学Spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.3 【第二章】 IoC 之 2.2 IoC 容器基本原理 ——跟我学Spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.4 【第三章】 DI 之 3.1 DI的配置使用 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 1.5 【第三章】 DI 之 3.2 循环依赖 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 1.6 【第三章】 DI 之 3.1 DI的配置使用 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 1.7 【第三章】 DI 之 3.2 循环依赖 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 1.8 【第三章】 DI 之 3.3 更多DI的知识 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104 1.9 【第三章】 DI 之 3.4 Bean的作用域 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121 1.10 »Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了) . . . . . . . . . . . . . .132 1.11 【第四章】 资源 之 4.1 基础知识 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153 1.12 【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . .156 1.13 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 1.14 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . .171 1.15 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3 . . . . . . . . . . . . . . .177 1.16 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . .183 1.17 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3 . . . . . . . . . . . . . . . .197 1.18 【第章】 AOP 之 6.1 AOP基础 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 1.19 【第章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . .2

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大·风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值