spring基于注解的@Autowired,@Component,@Configuration,@Service

@Autowired可以应用到 Bean 的属性变量、属性的 setter 方法、非 setter 方法及构造函数等,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。还需要在xml文件里指定bean并通过 <context:component-scan base-package="entity,factory"/>扫描bean所在的包

image

@Component 为spring内置容器,相当于将该类自动注解到spring容器中,无需再xml文件里配置bean,并通过 <context:component-scan base-package="entity,factory"/>扫描bean所在的包

@Configuration 为spring内置容器,相当于bspring的配置文件xml 可通过 ApplicationContext context = new AnnotationConfigApplicationContext(TargerClass);来获取spring的bean容器,无需配置xml,并通过 <context:component-scan base-package="entity,factory"/>扫描bean所在的包

@Service和Component类似

posted @ 2021-11-13 21:56  语梦·添香  阅读(89)  评论(0编辑  收藏  举报