spring @Component注解原理是什么

发布时间:2021-11-15 11:50:17 作者:iii
来源:亿速云 阅读:225

本篇内容主要讲解“spring @Component注解原理是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring @Component注解原理是什么”吧!

1.@controller 控制器(注入服务)

2.@service 业务(注入dao)

3.@repository dao(实现dao访问)

4.@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

5.@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。

@Servicepublic class UserServiceImpl implements UserService {} @Repositorypublic class UserDaoImpl implements UserDao {}

6.<context:annotation-config />

这样就可以使用@ Resource 、@ PostConstruct、@ PreDestroy、@PersistenceContext、@Autowired、@Required等注解了,就可以实现自动注入

7.<context:component-scan base-package="com.**.impl"/>

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.spring ioc控制反转

<--spring容器控制对象资源属性--><bean id="batchNo"class="com.common.utils.IdGenerator"><constructor-arg name="datacenterId"value="${server.datacenterId}"></constructor-arg>  --私有成员变量<constructor-arg name="workerId"value="${server.workerId}"></constructor-arg></bean>

<--spring依赖注入对象-->@Autowired@Qualifier("batchNo")private IdGenerator idGenerator;

相当于使用@Component("")

到此,相信大家对“spring @Component注解原理是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Spring多种加载Bean方式简析
  2. spring @Component注解原理解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring @component

上一篇:如何使用python中的[:-1]和[::-1]

下一篇:Android倒计时神器分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》