spring概念简介、bean扫描与注册实现方式
2017-03-04 23:20
441 查看
写在前面:本文作为整理,包含很多个人理解,有跳跃成份,初学者如果看晕了,可以先看其它同类文章,或者……多看几遍。
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:面向切面编程,具体概念略,实现看后续整理
2、注解方式,自动扫描
1)、现在spring的xml文件中开启注解扫描以及配置扫描的范围:
1
添加扫描过滤:
延伸部分:在spring中,
需要注意的是它并没有激活@Transactional 和 @TransactionAttribute
- 激活@ExceptionHandler这个annotation
- 配置了这个标签还可以将RequestMappingHandlerAdapter注册到Spring中
- 是SpringMVC提供默认的类型转化,因为我们没有在
2)、注解bean,以便被Spring容器扫描并实现bean注册
在类上添加@Component,@Repository,@Service,@Controller等注解,其中:
注意:注解方式bean的名称可以在注解时手动指定,比如@Controller(“testAnnotationController”),如果不指定则bean名称是由BeanNameGenerator生成的,格式为类名称首字母小写其它不变
一、概念部分:
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扫描与注册实现方式
- spring概念简介、bean扫描与注册实现方式
- Spring整理系列(01)——spring概念简介、bean扫描与注册实现方式
- (8) 让Spring自动扫描和管理Bean ---- 通过在classpath自动扫描方式把组件纳入spring容器中管理
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- Spring进阶之路(12)-注解方式实现Bean的注入
- Spring中Bean的注册与注入——XML方式
- (7) 让Spring自动扫描和管理Bean ---- 通过在classpath自动扫描方式把组件纳入spring容器中管理
- Spring注解方式使用与注册bean
- spring aop相关概念和xml与注解实现方式
- Spring-实现FactoryBean接口的方式配置Bean
- 通过注解的方式在spring中注册bean
- Spring中的Bean实现的创建三种方式
- Spring-装配Bean的3种方式-实现工厂方法方式装配Bean
- 【Spring学习】Bean的扫描注册
- spring 简介及bean的创建方式
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- Spring Bean注册方式小结
- Spring AOP实现方式三之自动扫描注入【附源码】
- Spring实现Bean动态注册