优雅的使用Spring

Bean声明的三种方式:

1、@Component, @Service, @Repository,@Controller 用于声明一个组件,程序启动时会扫描这些组件,并创建实例。

2、在applicationContext-module.xml 中使用<bean/>来声明

3、在@Configuration标注的类中使用@Bean来声明Bean

第1种方式,常用于主模块中,也可以认为是项目的最顶层模块中的类。他们常于

@Resource,@Autowired @Inject 配合使用。

第2、3方式,常用与需要配置的类、声明非顶层模块的类。常与properies,environment,yaml,@Property,<property/>结合使用。

原文地址:https://www.cnblogs.com/f1194361820/p/8847832.html