@ComponentScan 和 @Configuration

2024-08-28 06:08

本文主要是介绍@ComponentScan 和 @Configuration,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@ComponentScan 如果不设置basePackage的话 默认会扫描包的所有类,所以最好还是写上basePackage ,减少加载时间。默认扫描**/*.class路径 比如这个注解在com.wuhulala 下面 ,那么会扫描这个包下的所有类还有子包的所有类,比如com.wuhulala.service包的应用

@Configuration 表示这个类是一个spring 配置类,一般这里面会定义Bean,会把这个类中bean加载到spring容器中

@EnableAutoConfiguration springboot的注解 会在你开启某些功能的时候自动配置
,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用,并对Spring进行相应地设置。


我们通常建议您在其他类的根包中找到主应用程序类。@EnableAutoConfiguration 注解通常会放在 main class上, 这为特定的东西隐式的定义了一个基础的“search package”. 比如, 如果你在写一个 JPA application, 有使用了@EnableAutoConfiguration 注解的class的包 回去查找 @Entity 的items.


We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @EnableAutoConfiguration annotated class will be used to search for @Entity items.


@SpringBootApplication 相当于 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三个的作用

@ComponentScan
@Configuration
public class AppConfig {@Beanpublic Foo foo() {return new Foo(bar());}@Beanpublic Bar bar() {return new Bar();}}

这篇关于@ComponentScan 和 @Configuration的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1113944

相关文章

Pycharm安装报错:Cannot detect a launch configuration解决办法

《Pycharm安装报错:Cannotdetectalaunchconfiguration解决办法》本文主要介绍了Pycharm安装报错:Cannotdetectalaunchconfigur... 本文主要介绍了Pycharm安装报错:Cannot detect a launch configuratio

MyBatis的配置对象Configuration作用及说明

《MyBatis的配置对象Configuration作用及说明》MyBatis的Configuration对象是MyBatis的核心配置对象,它包含了MyBatis运行时所需的几乎所有配置信息,这个对... 目录MyBATis配置对象Configuration作用Configuration 对象的主要作用C

MySQL 中的服务器配置和状态详解(MySQL Server Configuration and Status)

《MySQL中的服务器配置和状态详解(MySQLServerConfigurationandStatus)》MySQL服务器配置和状态设置包括服务器选项、系统变量和状态变量三个方面,可以通过... 目录mysql 之服务器配置和状态1 MySQL 架构和性能优化1.1 服务器配置和状态1.1.1 服务器选项

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

关于@MapperScan和@ComponentScan的使用问题

《关于@MapperScan和@ComponentScan的使用问题》文章介绍了在使用`@MapperScan`和`@ComponentScan`时可能会遇到的包扫描冲突问题,并提供了解决方法,同时,... 目录@MapperScan和@ComponentScan的使用问题报错如下原因解决办法课外拓展总结@

How can I load the openai api configuration through js in html?

题意:怎样在HTML中通过JavaScript加载OpenAI API配置 问题背景: I am trying to send a request through js in my html so that openai analyzes it and sends a response, but if in the js I put the following: 我正在尝试通过HTM

单刀直入@ComponentScan之 资源加载

欢迎大家入坑,所谓师傅领进坑爬出去靠个人,首先我要说的是这个是上一篇《单刀直入@ComponentScan》的姊妹篇哈,接着把没聊透的事说明白,咱不是虎头蛇尾的人。 资源加载是啥意思 scan ,都认识吧,小学词汇连我都认识,扫到的是啥,扫到的是资源啊,如何让资源为我所用,就需要把资源搞进来,这就是资源加载。 spring如何加载资源的 首先不得不承认spring本身是很专一的,她把所有的

单刀直入ComponentScan

何为单刀直入,为何单刀直入 以大家都知道的一个知识点,逐层剖析最终与spring的核心框架相遇,很多讲解spring源码的文章都是上来一大堆晦涩难懂的基础知识,结果让人望而却步,本系列文章旨在由一个每个人都知道的点并且平时都会用到的知识,剖析spring实现的方式,从而与spring诸多的概念相遇,从具体的应用再到概念的理解,面窄一些但是更容易懂一些。 今天说的就是 @interface Co

@ComponentScan注解

作用与功能‌:‌ -‌自动扫描组件‌:‌@ComponentScan注解用于指定Spring在初始化时应该扫描哪些包来寻找带有@Component、‌@Service、‌@Repository、‌@Controller等注解的类,‌并把这些类注册为Bean。‌ -‌重要属性‌:‌ -‌basePackages‌:‌指定需要扫描的包名或类,‌Spring会扫描这些包及其子包下的注解。‌ -‌bas

编译u-boot报错configuration written to .config

编译u-boot报错 1.报错显示 1.报错显示 ## configuration written to .config#scripts/kconfig/conf --silentoldconfig Kconfig*** Error during update of the configuration.scripts/kconfig/Makefile: