Spring整理系列(01)——spring概念简介、bean扫描与注册实现方式
来源:互联网 发布:知乎的live 编辑:程序博客网 时间:2024/04/28 21:13
写在前面:本文作为整理,包含很多个人理解,有跳跃成份,初学者如果看晕了,可以先看其它同类文章,或者……多看几遍。
一、概念部分:
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>
添加扫描过滤:
<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)
@Controllerpublic class TestAnnotationController {...}
注意:注解方式bean的名称可以在注解时手动指定,比如@Controller(“testAnnotationController”),如果不指定则bean名称是由BeanNameGenerator生成的,格式为类名称首字母小写其它不变
- Spring整理系列(01)——spring概念简介、bean扫描与注册实现方式
- Spring概念简介、bean扫描与注册实现方式
- Spring整理系列(11)——@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
- Spring注解方式使用与注册bean
- Spring整理系列(05)——spring bean的生命周期
- Spring Bean 相关概念整理
- Spring Bean注册方式小结
- Spring中Bean的注册与注入——XML方式
- Spring中Bean的注册与注入——注解方式
- 【Spring学习】Bean的扫描注册
- Spring整理系列(13)——基于配置方式实现spring AOP
- 读懂Spring核心系列1(注册与获取bean)
- spring实现动态注册bean
- Spring实现Bean动态注册
- Spring 实现动态注册Bean
- Spring源码——BeanFactory 注册bean
- Spring整理系列(11)——@Configuration注解、@Bean注解以及配置自动扫描、bean作用域 发表于2016/8/11 15:16:16 10628人阅读 分类: Spring
- Spring整理系列(04)——spring bean的作用域
- [总结]FFMPEG视音频编解码零基础学习方法
- CodeForces 459D Pashmak and Parmida's problem
- OkHttp调用HTTPS遇到的问题之:SSLPeerUnverifiedException
- 一些常用的cmd命令
- 编程王道,唯“慢”不破(好文章)
- Spring整理系列(01)——spring概念简介、bean扫描与注册实现方式
- C# ==、Equals、ReferenceEquals 区别与联系
- Android自学之路
- Huawei LiteOS内核剖析0--启动
- #windows下搭IPython notebook环境
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(离线线段树)
- Android开发--重点知识汇总(持续更新)
- js(javaScript)学习系列--DOM
- 数据结构 - 单链表