@Component注解、@ComponentScan注解

ゞ 浴缸里的玫瑰 2022-04-22 08:52 419阅读 0赞

参考资料:《Spring 实战4》

@Component 注解表明该类会作为组件类,并告知Spring要为这个类创建bean。

组件扫描默认是不开启的。我们需要显示的配置一下Spring, 从而命令他寻找带有@component注解的类,并为其创建bean.

  1. @Configuration
  2. @componentScan
  3. public class CDPlayerConfig {
  4. }

@Configuration,指明这个是一个配置类。
@ComponentScan,这个注解能够在spring中启动扫描。

如果没有其他配置,@ComponentScan 默认会扫描与配置文件相同的包。查找包下带有@Component 注解的类,并会在Spring 中自动为其创建一个bean。

注:@Named 注解跟@ComponentScan 注解功能类似,但名称定义不好,以后只用@ComponentScan 注解。

发表评论

表情:
评论列表 (有 0 条评论,419人围观)

还没有评论,来说两句吧...

相关阅读