spring概念简介、bean扫描与注册实现方式
写在前面:本文作为整理,包含很多个人理解,有跳跃成份,初学者如果看晕了,可以先看其它同类文章,或者……多看几遍。
一、概念部分:
1、spring概念:网上有很多
2、spring核心:IOC(DI)和AOP
3、IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护,只是负责使用
解释一下就是:原来你在A类里面使用B类,需要new B(),现在不用new了,new对象的过程交给外部容器(Spring容器,它把所有的对象都称作为Bean)实现控制权转移,A类只是负责使用
4、DI:依赖注入,是IOC的一种实现方式,目的:创建对象并且组装对象之间的关系
5、创建对象并且组装对象之间的关系,这是两个过程:
1)、创建对象可以称为bean的扫描、注册,可通过xml配置和注解两种方式实现
2)、组装对象之间的依赖关系称为注入,注入方式一般分为:setter注入和构造器注入,依据形式不同又分为xml配置注入、xml配置自动装配、注解自动装配
6、AOP:面向切面编程,具体概念略,实现看后续整理
二、bean的扫描、注册
1、xml配置(schema)方式,手动扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 定义注册User的bean,唯一名称为user -->
<bean id="user" class="com.test.User"></bean> <!-- 定义注册Dept的bean,唯一名称为dept -->
<bean name="dept" class="com.test.Dept"></bean> </beans>
2、注解方式,自动扫描
1)、现在spring的xml文件中开启注解扫描以及配置扫描的范围:<context:component-scan base-package="">标签
<context:component-scan base-package="com.test"></context:component-scan>
- 1
添加扫描过滤:
<context:component-scan base-package="com.test">
<!-- 只扫描com.test包及子包下的注解为Service的类,而过滤注解为Controller的类 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
延伸部分:在spring中,<context:annotation-config/>标签作用也是开启注解,它与<context:component-scan/>标签的区别是什么(还有一个’’)???
<context:annotation-config/> 标签告诉Spring到bean类中寻找一些annotation定义的类, 比如@Autowired @PostConstruct @PreDestroy @Resource 等。
需要注意的是它并没有激活@Transactional 和 @TransactionAttribute
<context:component-scan/>标签告诉Spring搜索指定包下面以及一些需要被自动注入的bean,比如@Component @Repository @Service @Controller,而<context:component-scan>标签功能包含<context:annotation-config>的功能。
<mvc:annotation-driven/>这个标签的作用之一就是在springMVC中告诉Spring去检测RequestMapping。其他的作用如下:
- 激活@ExceptionHandler这个annotation
- 配置了这个标签还可以将RequestMappingHandlerAdapter注册到Spring中
- 是SpringMVC提供默认的类型转化,因为我们没有在<mvc:annotation-driven/> 的属性中配置ConversionService
2)、注解bean,以便被Spring容器扫描并实现bean注册
在类上添加@Component,@Repository,@Service,@Controller等注解,其中:
@Component是一个通用注解,可用于任何bean
@Repository,@Service,@Controller是更有针对性的注解
@Repository通常用于注解DAO类,即持久层
@Service通常用于注解Service类,即服务层
@Controller通常用于Controller类,即控制层(MVC)
@Controller
public class TestAnnotationController {...}
注意:注解方式bean的名称可以在注解时手动指定,比如@Controller(“testAnnotationController”),如果不指定则bean名称是由BeanNameGenerator生成的,格式为类名称首字母小写其它不变
spring概念简介、bean扫描与注册实现方式的更多相关文章
- Solon 开发,四、Bean 扫描的三种方式
Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...
- spring中创建bean对象的三种方式以及作用范围
时间:2020/02/02 一.在spring的xml配置文件中创建bean对象的三种方式: 1.使用默认构造函数创建.在spring的配置文件中使用bean标签,配以id和class属性之后,且没有 ...
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- Spring中默认bean名称的生成策略/方式修改
最近公司项目打算模块化,其实一个原因也是为了能够整合公司多个业务的代码,比如一个资源xxx,两个业务中都有对这个资源的管理,虽然是一个资源,但是是完全不同的定义.完全不同的表.不同的处理逻辑.所以打算 ...
- Spring(五)核心容器 - 注册 Bean、BeanDefinitionRegistry 简介
目录 前言 正文 1.BeanDefinitionRegistry 简介 2.registerBeanDefinition 方法注册 Bean 最后 前言 上篇文章我们对 BeanDefinition ...
- 品Spring:负责bean定义注册的两个“排头兵”
别看Spring现在玩的这么花,其实它的“筹码”就两个,“容器”和“bean定义”. 只有先把bean定义注册到容器里,后续的一切可能才有可能成为可能. 所以在进阶的路上如果要想走的顺畅些,彻底搞清楚 ...
- spring注解扫描组件注册
最近对单点系统进行微服务拆分,被各个springboot的组件注册搞得云里雾里的.(有的是通过springboot的自动配置进IOC容器的,有的是自己添加构造方法添加进IOC容器.)决定抽时间将spr ...
- Spring4- 01 - Spring框架简介及官方压缩包目录介绍- Spring IoC 的概念 - Spring hello world环境搭建
一. Spring 框架简介及官方压缩包目录介绍 主要发明者:Rod Johnson 轮子理论推崇者: 2.1 轮子理论:不用重复发明轮子. 2.2 IT 行业:直接使用写好的代码. Spring 框 ...
- 使用spring配置类代替xml配置文件注册bean类
spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...
随机推荐
- matlab画直线,指定斜率与x坐标范围
闲话不说,直接上代码与图的效果!
- 负载均衡---在window与linux下配置nginx
最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...
- Python学习(六)模块 —— 第三方库
Python 第三方库 安装第三方库 在Python中,安装第三方库包,是通过setuptools这个工具完成的.Python有两个封装了setuptools的包管理工具:easy_install和p ...
- iOS:Xcode7以上版本安装镜像文件.dmg
Xcode:7.0~7.3的镜像如下,点击直接下载安装 xcode7.0:https://developer.apple.com/services-account/download?path=/Dev ...
- [Todo] Nodejs学习及Spider实验(包括php入门学习、React入门学习)
/Users/baidu/Documents/Data/Interview/Web-Server开发 深入浅出Node.js-f46c http://blog.csdn.net/u012273376/ ...
- Servlet学习笔记(一):生命周期
一.Servlet 生命周期: Servlet 生命周期可被定义为从创建直到毁灭的整个过程.以下是 Servlet 遵循的过程:初始化——响应请求——终止——回收 Servlet 通过调用 init ...
- [Compose] 19. Leapfrogging types with Traversable
We use the traversable instance on List to reimplement Promise.all() type functionality. For example ...
- strcpy与strncpy
char aa[]="123456789123456789123456789"; char bb[4]={0}; 1.strcpy(bb,aa); bb的空间,不能存下aa的内容, ...
- Python Post and Get 登陆web后台系统并抓取页面
#coding=utf8 #! /usr/bin/env python import httplib import re import socket import urllib timeout = 6 ...
- es5 - array - unshift
/** * 描述:该unshift()方法从数组中添加单个或多个元素,并且返回长度 * 语法:arr.unshift(element1 [,... [,elementN ]]) * 参数:要添加到数组 ...