Spring
简介
- 2002,首次推出Spring框架的雏形,interface21框架
- Spring框架即以interface21框架为基础,经过重新设计,于2004.3.24发布了1.0正式版
- Rod Johnson Spring Framework创始人
- Spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架
ssh:Struct2+Spring+Hibernate
ssm:SpringMVC+Spring+Mybatis
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.18</version>
</dependency>
优点
- Spring是一个开源框架(容器)
- Spring是一个轻量级、非入侵式的框架
- 控制反转(IOC),面向切面编程(AOP)
- 支持事务的处理,对框架整合的支持
总结:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(IOC)的框架
组成
拓展
Spring Boot
- 一个快速开发的脚手架
- 基于SpringBoot可以快速的开发单个微服务
- 约定大于配置
Spring Cloud
- 基于SpringBoot实现的
IOC理论推导
1.UserDao接口
2.UserDaoImpl实现类
3.UserService业务接口
4.UserServiceImpl业务实现类
在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改源代码!如果程序代码量十分大,修改一次的成本代价十分昂贵。
我们使用一个Set接口实现,已经发生革命性变化!
//利用set进行动态实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
- 之前程序主动创建对象!控制权再程序员手上
- 使用set注入后,程序不再具有主动性,而是变成了被动的接受对象
这种思想从本质上解决问题,程序员不需要再去管理对象的创建。
系统的耦合性大大降低~可以更加专注在业务的实现上!这就是IOC的原型!
IOC本质
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。
HelloSpring
IOC创建对象的方式
1.默认使用无参构造创建对象
2.使用有参构造
- 下标赋值
<!-- 第一种,下标赋值-->
<bean id="user" class="com.ml.pojo.User">
<constructor-arg index="0" value="Miaolin"/>
</bean>
- 通过类型创建
<!-- 第二种,通过类型创建,不建议使用-->
<bean id="user" class="com.ml.pojo.User">
<constructor-arg type="java.lang.String" value="苗林l"/>
</bean>
- 直接通过参数名
<!-- 第二三种,直接通过参数名-->
<bean id="user" class="com.ml.pojo.User">
<constructor-arg name="name" value="苗林1234"/>
</bean>
配置文件在加载的时候,容器中对象就已经被初始化了
Spring配置
别名
<!-- alias 别名-->
<alias name="user" alias="user2"/>
Bean配置
<!--
id :bean 的唯一标识符,也就是我们学的对象名
class:bean对象所对应的全限定名:包名+类型
name:也就是别名,可以取多个
-->
<bean id="user" class="com.ml.pojo.User" name="user3,u3">
<property name="name" value="苗林1234"/>
</bean>
import
一般用于团队开发使用,可以将多个配置文件,导入合并为一个
一般在applicationContext.xml中导入
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
依赖注入
构造器注入
之前讲过
Set方式注入(重点)
- 依赖注入:Set注入
- 依赖:bean对象中的创建依赖于容器!
- 注入:bean对象中所有属性,由容器来注入
环境搭建
1.复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> bobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
//get set tostirng 自己生成,这里不复制了
}
3.applicationContext.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.ml.pojo.Address">
<property name="address" value="北京"/>
</bean>
<!-- 第一种,普通值注入,value-->
<bean id="student" class="com.ml.pojo.Student">
<property name="name" value="苗林"/>
<!-- Bean注入,ref-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<!-- list-->
<property name="bobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
</list>
</property>
<!-- Map-->
<property name="card">
<map>
<entry key="身份证" value="11111111"/>
<entry key="学号" value="22222222"/>
</map>
</property>
<!-- Set-->
<property name="games">
<set>
<value>LOL</value>
<value>WOW</value>
<value>DNF</value>
</set>
</property>
<!-- null-->
<property name="wife">
<null/>
</property>
<!-- properties-->
<property name="info">
<props>
<prop key="url">男</prop>
<prop key="name">大熊</prop>
</props>
</property>
</bean>
</beans>
4.测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
拓展方式注入
我们可以使用p命名空间和c命名空间注入
<!-- p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.ml.pojo.User" p:name="秒零零" p:age="18"/>
<!--c命名空间注入,需要实体类有参构造-->
<bean id="user2" class="com.ml.pojo.User" c:name="苗林啦啦啦" c:age="17"/>
测试:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user2");
System.out.println(user);
}
注意点:c和p命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"