[Spring Framework]学习笔记--@Component等stereotype的基础
在继续讲解Spring MVC之前,需要说一下常用的几个用来标记stereotype的annotation。
@Component,@Controller,@Repository,@Service。
这四个都在org.springframework.stereotype包下面,后面3个都属于@Component。
可以理解为@Component是@Controller,@Repository,@Service的基类。
@Component是用来标记任何被Spring管理的组件。
@Controller用来标记presentation层(比如web controller)。
@Repository用来标记persistence层(比如DAO)。
@Service用来标记service层。
如果我们为自己class增加了这些annotation后,如果让Spring自动找到这些class,并实现注册呢?
需要在Spring的xml中用到<context:component-scan>元素, base-package是要扫描的包名。
<beans
...
xmlns:context="http://www.springframework.org/schema/context"
...>
<context:component-scan base-package="xxx.xxx"/>
</beans>
<context:component-scan>的使用,是默认激活<context:annotation-config>功能的。而<context:annotation-config>又是干啥的呢?
主要是为@Autowired服务的(换句话就是说,使Spring可以处理像@Autowired和@Configuration这样的annotations),试想一下,如果没有<context:annotation-config>, 我们需要在Spring的XML文件中去注册每一个bean,
导致整个XML文件非常大,而且难以维护。现在用一行,就可以来实现注册,简单方便。
注意1:如果有了<context:component-scan>, 我们是不需要显式的来定义<context:annotation-config>的。
注意2:<context:annotation-config>只搜索在同一个application context下的被annotation标记的beans。
举个例子,就是如果<context:annotation-config>加在WebApplicationContext下,它只检查在controller中被标记为Autowired的beans,不会检查service中的。
[Spring Framework]学习笔记--@Component等stereotype的基础的更多相关文章
- Spring Framework 学习笔记——核心技术之Spring IOC
Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...
- [Spring Framework]学习笔记--Dependency injection(DI)
1. 通过构造函数实现DI 简单类型实例 package examples; public class ExampleBean { // Number of years to calculate th ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
- Spring框架学习笔记(5)——Spring Boot创建与使用
Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
随机推荐
- Element 'beans' cannot have character [children]
在编写spring的applicationContext.xml文件时,出现了: Element 'beans' cannot have character [children], because t ...
- Epplus 设置excel 页边距 及多文件合并
1:使用epplus合并多个excel文件到同一excel的不同sheet页中 private static bool MergeExcel(string _stFilePath, List<s ...
- 最新office2003密钥
Microsoft Office Professional Edition 2003GWH28-DGCMP-P6RC4-6J4MT-3HFDY Office2003序列号注册码sn: WFDWY-XQ ...
- 彻底解决Odoo8.0单时区应用中的时区问题
原文地址:http://shine-it.net/index.php/topic,17001.0.html 由于数据库中存储的是UTC时区,默认情况下数据导出和group by都存在时区问题.彻底解决 ...
- go语言基础之基匿名函数本语法和闭包
一.匿名函数 示例1: package main import "fmt" func main() { a := 10 str := "mike" //匿名函数 ...
- 为什么Domain controller上的time synchronization非常重要?
虚拟机默认情况下所拥有的资源都是不同的, 比如说CPU clock. 在一个忙碌的系统中, 虚拟机甚至可能在很短的一段时间内被拒绝分配资源给它, 这种情况还可能发生在高系统负荷, VMotion, B ...
- C# 使用int.TryParse,Convert.ToInt32,(int)将浮点类型转换整数时的区别
int.TryParse,Convert.ToInt32,(int) 这几种类型在将浮点类型转换整数时是有差别 Convert.ToInt32则会进行四舍五入 int.TryParse只能转换整数,即 ...
- (转)AS3 中,Function.apply、call中第一个参数的作用;与什么时候用
http://blog.csdn.net/linjf520/article/details/8746064 大家在使用Function.apply或是call时,是否发现,第一个参数不知道怎么用,赋值 ...
- GameCenter 使用指南
原地址:http://www.cocoachina.com/gamedev/misc/2010/1022/2229.html GameCenter 为单机游戏为主的 iPhone 游戏平台引入了社会化 ...
- vue2.x 总结
1.独立构建vs运行时构建 在按照vue1.0的配置配置好webpack后,会出现Failed to mount component: template or render function not ...