Spring - SpringIOC容器详解
一、什么是Spring IOC:
Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。
在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。
二、Spring中如何实现DI(依赖注入)
1.构造器注入
<bean id="beanName" class="BeanClassName">
<constructor-arg index="构造器中的位置" name="参数名" value="简单值" type="Java类型"/>
<constructor-arg index="构造器中的位置" name="参数名" ref="OtherBeanName" type="Java类型"/>
</bean>
2.Setter注入
<bean id="beanName" class="BeanClassName">
<property name="字段名" value="简单值">
<property name="字段名" ref="OtherBeanName">
</bean>
3.接口注入:不常用,例如JNDI注入tomcat自带数据库连接池
三、XML配置Bean
0.命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
1.装配简易值和对象
<bean id="beanName" class="类的全限定名"> <property name="字段名" value="简易值"/> <property name="字段名" ref="otherBeanName"/> </bean>
2.装配集合
<bean id="beanName" class="类的全限定名"> <property name="字段名set"> <set> <value>value</value> //<ref bean="otherBeanName"> </set> </property> <property name="字段名list"> <list> <value>value</value> //<ref bean="otherBeanName"> </list> </property> <property name="字段名props"> <props> <prop key="name">value</prop> </props> </property> <property name="字段名map"> <map> <entry key="key" value="value"/> //<entry key="key" value-ref="otherBeanName"/> //<entry key-ref="otherBeanName" value-ref="otherBeanName"/> </map> </property> <property name="数组"> <array> <value>value1</value> </array> </property> </bean>
四、注解配置Bean
1.@Component 配置Bean
配置在类上,代表这个类会被SpringIOC扫描成一个Bean,注解中属性value表示为Bean的名字,如同XML中配置的id,不配置
value的值时,默认是将类名的首字母小写。
@Component
@Component("beanName")
2.@ComponentScan 配置注解扫描器
配置在类上,因为SpringIOC容器并不知道要扫描哪个类和包,需要定义一个SpringConfig类来告诉它,注解默认扫描该类所在包
如果想扫描指定包,注解属性basePackages中,多个包,使用逗号隔开
@ComponentScan
@ComponentScan(basePackages={"包1","包2"})
3.@Value 简单值注入
配置在类的字段(属性)上,其中放入的是字符串,注入时会自动转换类型,同样可以使用EL表达式来注入资源属性文件中的值
@Value("1")
@Value("xxxx")
@Value("${jdbc.database.driver}")
4.@Autowired 自动装配属性
配置在类的字段(属性)上、方法及构造函数的参数中完成自动装配的工作,可以通过 @Autowired的使用来消除 setter ,getter方法
此注解是按照类型来注入的,当有多个bean同时实现一个接口时,可以使用@Primary和@Qualifier注解来消除歧义。
@Autowired
5.@Primary 优先注入
配置在某个接口有多个实现类的某个实现类上,在其他Bean中使用@Autowired注入该接口类型的bean时,优先注入这个实现类的bean。
和@Component一起使用,否则无效。
6.@Qualifier 指定注入
配置在类的字段(属性)上,和@Autowired一起使用,当@Autowired注入时,指定注入的bean,该注解的属性(value)指定Bean的name
@Qualifier("beanName")
7.@Configration 配置菜单
配置在类上,此注解通常为了通过 @Bean 注解生成 SpringIOC容器管理的类,本质和@Component一样。
@Configration
8.@Bean 配置第三方类的Bean
配置在方法上,将方法返回的对象生成Bean,交给SpringIOC容器管理,要求此方法所在类也被配置成Bean,通常使用@Configration,
注解的属性name,指定生成的Bean的name
@Bean(name="beanName")
9.@Resource(name="beanName") jdk的自动装配
相当于@Autowired,区别在于Resource先按beanName来找注入的Bean,如果没有指定名字的Bean,再按照类型来注入。
五、启动SpringIOC容器的方式
1.Spring采用xml配置时,根据xml文件来启动
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-config.xml");ClassName cn = (ClassName)ac.getBean("beanName");
2.Spring采用注解配置时,根据Springconfig配置类来启动
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
ClassName cn = (ClassName)ac.getBean("beanName");
六、注解和XML的混合使用
1.注解引用XML
@ImportResource({"classpath:spring-config.xml","classpath:spring-config1.xml"}),配置在SpringConfig类上
2.多个注解配置类
@Import({SpringConfig1.class,SpringConfig2.class})
3.XML扫描注解
<context:component-scan base-packages="包名1,包名2"/>
4.多个XML文件
<import resource="spring-config2.xml"/><import resource="spring-config3.xml"/>
七、加载属性文件
· 1.XML加载资源属性文件
<context:property-placeholder ignore-resource-not-found="true" location="classpath:database-config.properties"/>
2.注解加载资源属性文件
@PropertySource(value={"classpath:database-config.properties"},ignoreResourceNotFound="true")
value={"classpath:database-config.properties"} : 资源属性文件位置
ignoreResourceNotFound="true" : 当扫描不到此资源配置文件时,不报错
八、Bean的作用域
Spring提供了4种作用域:singleton(单例) prototype(原型) Session(会话) request(请求)
XML中的bean中有属性:scope ,默认是单例,可以设置为原型。会话和请求的作用域需要在web.xml中配置
注解中有注解@Scope("singleton"),@Scope("prototype"),默认是单例,可以设置为原型。
Spring - SpringIOC容器详解的更多相关文章
- 三、spring成长之路——springIOC容器详解(上)
目录 一.springIOC 一.springIOC 控制反转和依赖注入: 简单的说就是将对象的创建,属性的的设置交给spring容器进行管理,而不再由用户自己创建,当用户需要使用该接口或者类的时 ...
- Spring ——Spring IoC容器详解(图示)
1.1 Spring IoC容器 从昨天的例子当中我们已经知道spring IoC容器的作用,它可以容纳我们所开发的各种Bean.并且我们可以从中获取各种发布在Spring IoC容器里的Bean,并 ...
- [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。
一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...
- Spring Boot异常处理详解
在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...
- spring注入参数详解
spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...
- Spring的lazy-init详解
1.Spring中lazy-init详解ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化(也就是依赖注入).提前实例化意味着作为初始 ...
- Spring Boot 配置文件详解
Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...
- (转)Spring事务管理详解
背景:之前一直在学习数据库中的相关事务,而忽略了spring中的事务配置,在阿里面试时候基本是惨败,这里做一个总结. 可能是最漂亮的Spring事务管理详解 https://github.com/Sn ...
- Spring IOC使用详解
SpringIOC使用详解 一.IOC简介 IOC(Inversion of Control):控制反转,即对象创建的问题.通俗地讲就是把创建对象的代码交给了Spring的配置文件来进行的.这样做的优 ...
随机推荐
- JS的事件流概念*******
事件的概念 HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件. 事件流 事件流描述的 ...
- web性能压力测试工具http_load/webbench/ad
http_load 下载地址:http://www.acme.com/software/http_load/http_load-12mar2006.tar.gz 程序非常小,解压后也不到100K 居家 ...
- L90
On Motes and Beams 微尘与栋梁 It is curious that our own offenses should seem so much less heinous than t ...
- usg6500
- 解决 sublime text3 运行python文件无法input的问题
怎么输入都没有用,原来需要配置可交互环境来运行 首先,Ctrl+Shift+p快捷键,弹出框框输入 install Package,回车后又弹出一个框,输入SublimeREPL(要安装的插件名字), ...
- ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)
Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...
- tyvj 1203 机器分配
时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 总公司拥有高效生产设备M台,准备分给下属的N个公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何 ...
- 【LeetCode】282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- BZOJ3127:[USACO2013OPEN]Yin and Yang
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...