目录
IOC:Inversion Of Control,即控制反转,是一种设计思想。
DI:Dependency injection,即依赖注入。
什么是Spring
- Spring是一个轻量级的开源免费框架。
- Spring的理念就是粘合剂,将各种框架整合在一起。
- Spring的核心是IOC和AOP。
Spring框架的组成
Spring的IOC/DI
IOC:Inversion Of Control,即控制反转,是一种设计思想。
它的作用就是:将主动创建对象的方式反转成被动接受, 实现代码的解耦。
DI:Dependency injection,即依赖注入。
依赖注入是IOC的一种实现。 在Spring中交由容器来管理所有对象。
IOC(控制反转)
控制反转--就是依赖倒置原则的一种代码设计的思路。
采用的方法就是所谓的依赖注入(DI)
依赖倒置原则
把原本的高层建筑依赖底层建筑“倒置”过来,变成底层建筑依赖高层建筑。高层建筑决定需要什么,底层去实现这样的需求,但是高层并不用管底层是怎么实现的。这样就不会出现前面的“牵一发动全身”的情况。
Spring容器
Spring 容器是 Spring 框架的核心。容器负责管理Bean对象。
容器的两种实现:
BeanFactory 容器:最简单的容器,给 DI 提供了基本的支持。
ApplicationContext 容器,继承的BeanFactory,拥有更丰富的功能。
容器提供的常用方法:
getBean():获取容器的中指定的bean对象。
containsBean():判断容器中是否存在指定的bean对象。
isPrototype():判断指定的bean是否为非单例对象。
isSingleton():判断指定的bean是否为单例对象。
ApplicationContext 容器接口
Spring容器中的顶层接口是BeanFactory。ApplicationContext是它的子接口。
ApplicationContext的三个主要的实现类:
- ClassPathXmlApplication:它是从类的根路径下加载xml配置文件(推荐用这种)。
- FileSystemXmlApplication: 它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。(但使用不灵活,不推荐)
- AnnotationConfigApplication:当我们使用注解配置容器对象时,需要使用此类来创建spring容器。它用来读取注解。(Springboot默认使用这个)
XML装配Bean
早期的Spring框架中都是采用XML的方式来装配Bean
- 创建一个xml文件application.xml
- 使用bean标签来装备Bean对象
-
<bean id="user" class="com.Yuu.bean.User"></bean>
通过容器来构造Bean对象
-
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); // 通过名字来获取Bean对象 User user = (User)ctx.getBean("user"); // 通过类型来获取Bean对象 User user1 = ctx.getBean(User.class);
Bean的作用域
默认情况下,Spring只为每个在IOC容器里声明的Bean创建唯一一个实例,整个IOC容器的范围内都能共享该实例,即单例模式。
在xml配置的bean标签上通过scope属性来设置作用域。
依赖注入方式
Bean对象中的属性值通过IOC容器进行注入,这种方式称为依赖注入。
依赖注入的方式有三种:
- 属性注入
- 构造器注入
- 工厂方法注入
属性注入
属性注入是通过setter方法注入
在xml中通过使用property标签来注入,其中name属性是对象中的属性名,value是属性值。
<bean id="user" class="com.Yuu.bean.User">
<property name="id" value="1" ></property>
<property name="name" value="Yuu" ></property>
<property name="age" value=“24" ></property>
</bean>
构造方法注入
1. 通过构造方法注入,可以在创建对象时就注入属性值。
在xml中通过使用constructor-arg标签来注入。
<bean id="user" class="com.Yuu.bean.User">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="Yuu"></constructor-arg>
<constructor-arg value="24"></constructor-arg>
</bean>
在User类中需要创建一个对应的构造方法。
2.通过index属性指定参数的顺序。
<bean id="user" class="com.Yuu.bean.User">
<constructor-arg value="1“ index=“0”></constructor-arg>
<constructor-arg value="Yuu “index=“1” ></constructor-arg>
<constructor-arg value="24“ index=“2” ></constructor-arg>
</bean>
3.通过类型来匹配参数。
<bean id="user" class="com.Yuu.bean.User">
<constructor-arg value="1" type="java.lang.String"></constructor-arg>
<constructor-arg value="Yuu" type="java.lang.String"></constructor-arg>
<constructor-arg value="24" type="int"></constructor-arg>
</bean>
4.字面值:可以通过value属性或value标签来注入。
若字面值包含特殊字符,可以用<![CDATA[]]>将字面值包裹起来。
<constructor-arg type="java.lang.String">
<value><![CDATA[<chengdu>]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>24</value>
</constructor-arg>
引用其他Bean
Bean对象中的属性不仅仅是基本类型或字符串,还有可能是其他Bean对象。
在XML文件中使用ref来引用其他Bean对象。
<bean id=“address” class=“com.Yuu.bean.Address”>
<constructor-arg value=“九眼桥” ></constructor-arg>
<constructor-arg value=“四川” ></constructor-arg>
<constructor-arg value=“成都” ></constructor-arg>
</bean><bean id=“user” class=“com.lovo.bean.User”>
...
<property name="address" ref="address">
</property>
</bean>
注入null值
如果是引用数据类型的注入可以使用null标签来注入空值。
<constructor-arg><null /></constructor-arg>
装配集合
集合使用list标签完成装配
<bean id="class" class="com.project.bean.ClassBean">
<property name="classId" value="1"></property>
<property name="className" value="终极一班"></property>
<property name="studentList">
<list>
<ref bean="stu1"></ref>
<ref bean="stu2"></ref>
<ref bean="stu3"></ref>
</list>
</property>
</bean>
注入Properties集合
使用props标签来注入java.util.Properties对象,prop是子标签,每个子标签必须定义key属性。
<bean id="baseDAO" class="com.Yuu.dao.BaseDAO">
<property name="dataSource">
<props>
<prop key="dbuser">root</prop>
<prop key="dbpass">123456</prop>
<prop key="dburl">jdbc:mysql://localhost:3306/demo</prop>
<prop key=“driverclass">org.gjt.mm.mysql.Driver</prop>
</props>
</property>
</bean>
使用外部属性文件
- 有时一些配置信息来自另外的文件(比如文件路径、数据源配置等等)。
- 将这些配置信息和Bean配置分离是常见的做法。
- Spring提供了BeanFactory的一个后置处理器PropertyPlaceholderConfigurer来读取外部属性文件,在Bean配置文件中使用${属性名}来访问对应的值。
Spring2.5以后,通过<context:property-placeholder>来加载外部文件。
<context:property-placeholder location="classpath:db.properties"/>
<bean id="baseDAO" class="com.Yuu.dao.BaseDAO">
<property name="dataSource">
<props>
<prop key="dbuser">${dbuser}</prop>
<prop key="dbpass">${dbuser}</prop>
<prop key="dburl">${dburl}</prop>
<prop key="driverclass">${driverclass}</prop>
</props>
</property>
</bean>