Spring源码-源码层面讲解bean标签添加了lookup-method和replaced-method标签之后源码执行流程,以及对象实例化的流程

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;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/404163.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言部分内存函数详解

C语言部分内存函数详解 前言1.memcpy1.1基本用法1.2注意事项**目标空间与原空间不能重叠****目标空间原数据会被覆盖****目标空间要够大****拷贝字节数需小于原空间大小** 1.3模拟实现 2.memmove2.1基本用法2.2注意事项2.3模拟实现 3.memset3.1基本用法 4.memcmp4.1基本用法4.2…

[论文笔记]ZeRO: Memory Optimizations Toward Training Trillion Parameter Models

引言 今天带来ZeRO: Memory Optimizations Toward Training Trillion Parameter Models的论文笔记。 大型深度模型提供了显著的准确性提升&#xff0c;但训练数十亿到数万亿个参数是具有挑战性的。现有的解决方案&#xff0c;如数据并行和模型并行&#xff0c;存在基本的局限…

python小游戏之摇骰子猜大小

最近学习Python的随机数&#xff0c;逻辑判断&#xff0c;循环的用法&#xff0c;就想找一些练习题&#xff0c;比如小游戏猜大小&#xff0c;程序思路如下&#xff1a; 附上源代码如下&#xff1a; 摇骰子的函数&#xff0c;这个函数其实并不需要传任何参数&#xff0c;调用后…

【Delphi】中多显示器操作基本知识点

提要&#xff1a; 目前随着计算机的发展&#xff0c;4K显示器已经逐步在普及&#xff0c;笔记本的显示器分辨率也都已经超过2K&#xff0c;多显示器更是普及速度很快。本文介绍下Delphi中操作多显示器的基本知识点&#xff08;Windows系统&#xff09;&#xff0c;这些知识点在…

【Java】 方法引用与Lambda(快速上手)

Java系列文章目录 补充内容 Windows通过SSH连接Linux 第一章 Linux基本命令的学习与Linux历史 文章目录 Java系列文章目录一、前言二、学习内容&#xff1a;三、问题描述四、解决方案&#xff1a;4.1 方法引用 五、总结&#xff1a; 一、前言 Calculator::plus看到::好奇有什…

使用多种机器学习模型进行情感分析

使用 TF-IDF 与贝叶斯分类器进行情感分析是一个常见且有效的组合&#xff0c;特别是在文本分类任务中。贝叶斯分类器&#xff08;通常是朴素贝叶斯分类器&#xff09;等机器学习模型具有计算简单、效率高的优点&#xff0c;且在文本分类任务中表现良好。接下来&#xff0c;我将…

eNSP 华为ACL配置

华为ACL配置 需求&#xff1a; 公司保证财务部数据安全&#xff0c;禁止研发部门和互联网访问财务服务器&#xff0c;但总裁办不受影响 R1&#xff1a; <Huawei>sys [Huawei]sys Router1 [Router1]undo info-center enable [Router1]int g1/0/0 [Router1-GigabitEth…

语音助手Verbi:科技创新的未来

今天&#xff0c;我要向大家介绍一个名为Verbi的语音助手项目。这是一个结合了多种先进技术的模块化语音助手应用程序&#xff0c;能够实现语音到文本、文本生成和文本到语音的全流程处理。通过这个项目&#xff0c;我们可以体验到尖端科技如何改变我们的日常生活。 Verbi的诞…

ubuntu配pip的源

临时使用源 pip install [包名] -i [pip源URL]# 示例 pip install pytest -i https://pypi.tuna.tsinghua.edu.cn/simple更换配置pip镜像源 step1&#xff1a;创建一个配置文件 mkdir ~/.pip/ cd .pip sudo vim pip.conf step2:填写源信息&#xff0c;保存并退出【:wq】 [g…

文件包含漏洞(一)

本文仅作为学习参考使用&#xff0c;本文作者对任何使用本文进行渗透攻击破坏不负任何责任。 一&#xff0c;漏洞简述。 文件包含漏洞&#xff0c;通常发生在Web应用程序中&#xff0c;特别是那些使用用户输入动态生成内容的部分。这种漏洞允许攻击者通过提交恶意的文件路径请…

10 个 C# 关键字和功能

在 Stack Overflow 调查中&#xff0c;C# 语言是排名第 5 位的编程语言。它广泛用于创建各种应用程序&#xff0c;范围从桌面到移动设备再到云原生。由于有如此多的语言关键字和功能&#xff0c;对于开发人员来说&#xff0c;要跟上新功能发布的最新信息将是一项艰巨的任务。本…

C语言——操作符详解

目录 1.操作符的分类 2.原码、反码和补码 3.移位操作符 3.1 左移操作符 3.2 右移操作符 4.位操作符 4.1 按位与& 4.2 按位或| 4.3 按位异或^ ​编辑 4.4 按位取反~ 4.5 应用题 4.5.1 题目&#xff1a;不能创建临时变量&#xff0c;实现两个整数的交换 4.5.2 …

本地下载安装WampServer结合内网穿透配置公网地址远程访问详细教程

文章目录 前言1.WampServer下载安装2.WampServer启动3.安装cpolar内网穿透3.1 注册账号3.2 下载cpolar客户端3.3 登录cpolar web ui管理界面3.4 创建公网地址 4.固定公网地址访问 前言 Wamp 是一个 Windows系统下的 Apache PHP Mysql 集成安装环境&#xff0c;是一组常用来…

FFMPEG推流器讲解

FFMPEG重要结构体的讲解 FFMPEG中有六个比较重要的结构体&#xff0c;分别是AVFormatContext、AVOutputFormat、 AVStream、AVCodec、AVCodecContext、AVPacket、AVFrame、AVIOContext结构体&#xff0c;这几个结构体是贯穿着整个FFMPEG核心功能。 AVFormatContext 这个结构…

nginx基础配置

1. https配置 首先在nginx.conf中配置https 2. 重定向 rewrite ^/(.*)$ https://www.sxl1.com/$1 permanent;3. 自动索引 autoindex on;4. 缓存 Nginx expire缓存配置: 缓存可以降低网站带宽&#xff0c;加速用户访问location ~ .*\.(gif|jpg|png)$ {expires 365d;roo…

react最好用的swiper插件和拖动插件 react-tiny-slider react-draggable

react移动端项目&#xff0c;其实有挺多的ui框架的&#xff0c;但是我们公司的项目&#xff0c;都是自己封装的ui库&#xff0c;又不可能为了一个轮播图就去再安装一个ui库 所以找了很多的轮播插件&#xff0c;都是不能满足需求 最后找到了它&#xff0c;react-tiny-slider&…

Windows Microsoft Edge 浏览器 配置【密码】

在浏览 Web 时&#xff0c;Microsoft Edge 可以轻松保存密码。 在桌面或移动设备上的 Edge 浏览器中输入新密码时&#xff0c;Microsoft Edge 会询问你是否要记住用户名和密码。 下次访问该网站时&#xff0c;浏览器将完成帐户信息的填写。 如果使用 Microsoft 帐户登录到 Edg…

redis集合若干记录

无序集合 redis通常使用字典结构保存集合数据&#xff0c;字典健存储集合元素&#xff0c;字典值为空。如果一个集合全为整数&#xff0c;使用字典就有点浪费了&#xff0c;redis使用intset保存。 插入元素到intset中 获取插入元素编码&#xff0c;如果插入元素编码级别高于int…

机器学习 之 sklearn的使用介绍和如何找到API

scikit-learn&#xff08;简称 sklearn&#xff09;是基于python语言的一个第三方机器学习库&#xff0c;它提供了简单而有效的工具来进行数据分析和建模。建立在numpy pandas SciPy和Malpotlib库上&#xff0c;下面是对如何使用 sklearn 以及如何找到其 API 的一个基本介绍&am…

仿RabbiteMq实现简易消息队列正式篇(需求分析)

TOC 目录 MQ的实现方法 RabbitMq中的相关概念 消息队列系统模块划分 总体划分 服务端模块 数据管理模块 虚拟机数据管理模块 交换机路由模块 消费者管理模块 信道&#xff08;通信&#xff09;管理模块 连接管理模块 服务端BrokerServer模块 客户端模块 消费者管…