栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022-1-29 SpringBoot day1细节

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022-1-29 SpringBoot day1细节

文章目录

一、配置类注解二、启动类详解

一、配置类注解

@Configuration vs @Component
@Configuration
//配置类 那么以下bean都会被代理 Configuration 对于已经创建的对象就不会再被创建了
//如果不想被代理 那么@Component

二、启动类详解

这个MainApplication启动类放到根包目录下,这样才能扫描到组件

@SpringBootApplication

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.context.TypeExcludeFilter.class}), @org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter.class})})

 

@EnableAutoConfiguration

在原生的Spring frameWork中,组件装配有三个阶段
@import注解:

    Spring 2.5+ @ComponentSpring 3.0+ 使用@Configuration+@BeanSpring 3.1+ @EnableXXX +@import

对于Bean比较少的情况 前两种就行了;
对于一个框架,有大量bean需要注入

新建注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@import({Apple.class, Banana.class})
public @interface EnableFruit {

}

然后在启动类添加相应注解,即可完成注入。

@ComponentScan

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721519.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号