【Spring】spring中存储Bean(对象)的相关注解及相关用法

五大注解:@Controller,@Service,@Repository,@Component,@Configuration

1、@controller 控制器(注入服务)
控制层,接收请求,对请求进⾏处理,并进⾏响应。用于标注控制层,相当于struts中的action层

2、@service 服务(注入dao)

业务逻辑层,处理具体的业务逻辑

3、@repository(实现dao访问)
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件.

4、@component (配置文件中的 )
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

5、@Configuration:配置层,处理项⽬中的⼀些配置信息

一,@Controller(控制器存储)

使用@Controller存储Bean的代码如下:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第1张图片

从Spring中获取对象

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第2张图片

可以看到代码的执行情况:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第3张图片

二,@Service(服务存储)

使用@Service存储Bean的代码如下:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第4张图片

读取Bean代码 

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第5张图片 观察运⾏结果,发现成功从Spring中获取到UserService对象,并执⾏UserService的sayHi⽅法

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第6张图片 

三, @Repository(仓库存储)

使用@Repository存储Bean

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第7张图片

读取Bean的代码: 

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第8张图片

运行结果:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第9张图片

四,@Component (组件注解)

使用@Component存储bean的代码:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第10张图片

读取bean中的代码:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第11张图片

 运行结果:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第12张图片

五, @Configuration(配置存储)

使用@Confuguration存储bean中代码

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第13张图片

读取bean中的代码

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第14张图片 运行结果:

【Spring】spring中存储Bean(对象)的相关注解及相关用法_第15张图片

你可能感兴趣的:(框架,spring,java,前端)