spring @Component注解原理解析
这篇文章主要介绍了spring@Component注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.@controller控制器(注入服务)
2.@service业务(注入dao)
3.@repositorydao(实现dao访问)
4.@component(把普通pojo实例化到spring容器中,相当于配置文件中的
5.@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
@Service publicclassUserServiceImplimplementsUserService{ } @Repository publicclassUserDaoImplimplementsUserDao{ }
6.
这样就可以使用@Resource、@PostConstruct、@PreDestroy、@PersistenceContext、@Autowired、@Required等注解了,就可以实现自动注入
7.
Spring给我们提供了context:annotation-config的简化的配置方式,自动帮助你完成声明,并且还自动搜索@Component,@Controller,@Service,@Repository等标注的类。
context:component-scan除了具有context:annotation-config的功能之外,context:component-scan还可以在指定的package下扫描以及注册javabean。还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。
因此当使用context:component-scan后,就可以将context:annotation-config移除。
8.springioc控制反转
<--spring容器控制对象资源属性-->--私有成员变量
<--spring依赖注入对象--> @Autowired @Qualifier("batchNo") privateIdGeneratoridGenerator;
相当于使用@Component("")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。