@Component注解、@ComponentScan注解

参考资料:《Spring 实战4》

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

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

@Configuration
@componentScan
public class CDPlayerConfig {
}

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

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

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

猜你喜欢

转载自blog.csdn.net/Pengxiaozhen1111/article/details/83690260