springboot学习(五十九) springboot中使用spring-context-indexer加快启动速度

Spring中@ComponentScan扫描的package包含的类越多的时候,Spring模式注解解析耗时就越长,服务启动时候就越长,针对此问题Spring提供了@Indexed注解来添加索引。
查看@Serive、@Controller、@Repository、@Component注解源码会发现已经自动添加了此注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	String value() default "";

}

在项目的添加spring-context-indexer的依赖
maven中:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

gradle中

annotationProcessor 'org.springframework:spring-context-indexer'

gradle4.5以下使用

compileOnly "org.springframework:spring-context-indexer"

这样启动效率就提升啦。
会发现在编译后生成了META-INF/spring.components文件
@Indexed,可以预编译,跟lombok一样在编译期处理。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常出现在使用 Pandas DataFrame 进行多级索引时,尝试用长度与索引级别数量不匹配的值进行赋值操作。 例如,如果你有一个形状为 (3, 2) 的 DataFrame,其有两个级别的列索引,你需要使用一个长度为 3 的数组或 Series 来为其一个列级别赋值,使用一个长度为 6 的数组或 Series 来为两个列级别同时赋值。 下面是一个示例: ```python import pandas as pd # 创建一个带有两个级别的列索引的 DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) df.columns = pd.MultiIndex.from_product([['level1', 'level2'], ['col1', 'col2']]) # 尝试使用长度不匹配的数组为其一列级别赋值 df.loc[:, ('level1', 'col1')] = [10, 11] # 报错:ValueError: cannot set using a multi-index selection indexer with a different length than the value # 使用长度匹配的数组为其一列级别赋值 df.loc[:, ('level1', 'col1')] = [10, 11, 12] # 成功 ``` 如果你需要同时为多个列级别赋值,则需要使用长度为级别数量乘积的数组或 Series。例如,如果你有两个级别的列索引,每个级别下有三个列名,则需要使用长度为 6 的数组或 Series 来同时为它们赋值。 ```python # 尝试使用长度不匹配的数组为两个列级别同时赋值 df.loc[:, [('level1', 'col1'), ('level2', 'col1')]] = [10, 11, 12] # 报错:ValueError: cannot set using a multi-index selection indexer with a different length than the value # 使用长度匹配的数组为两个列级别同时赋值 df.loc[:, [('level1', 'col1'), ('level2', 'col1')]] = [10, 11, 12, 13, 14, 15] # 成功 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值