1、Spring
1、简介
-
Spring:春天——>给软件行业带来了春天
-
2002,首次推出了Spring框架的雏形:interface2.1框架
-
Spring框架即以interface2.1框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版
-
Rod Johnson,Spring的缔造者,很难想象Rod Johnson的学历,真的让好多人大吃一惊,他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学
-
Spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架
-
SSH:Struct2 + Spring + Hibernate
-
SSM:SpringMVC + Spring + MyBatis
官网:https://spring.io/projects/spring-framework
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.22</version>
</dependency>
2、优点
- Spring是一个开源的免费的框架(容器)
- Spring是一个轻量级的,非入侵式的框架
- 控制反转(IOC),面向切面编程(AOP)
- 支持事务的处理,对框架整合的支持
总结:Spring是一个轻量级的控制反转(IOC),面向切面编程(AOP)的框架
3、组成
4、拓展
- Spring Boot
- 一个快速开发的脚手架
- 基于SpringBoot可以快速开发单个微服务
- 约定大于配置
- Spring Cloud
- SpringCloud是基于SpringBoot实现的
因为现在的大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要完全掌握Spring及SpringMVC,承上启下的作用
弊端:发展了太久之后,违背了原来的理念,配置十分繁琐,人称配置地狱
2、IOC理论推导
1.UserDao 接口
2.UserDaoImpl 实现类
3.Uservice 接口
4.UserServiceImpl 实现类
在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要去修改源代码,如果程序代码量十分大,修改一次的成本代价十分昂贵
我们使用一个Set接口实现
public class UserServiceImpl implements UserService{
private UserDao userDao;
//利用Set进行动态实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
public class MyTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层它们不需要接触
UserService userService = new UserServiceImpl();
((UserServiceImpl)userService).setUserDao(new UserDaoMysqlImpl());
userService.getUser();
}
}
- 之前,程序是主动创建对象,控制权在程序员手上
- 使用了set注入后,程序不再具有主动性,而是变成了被动的接收对象
这种思想从本质上解决了问题,我们程序员不用再去管理对象的创建了。系统的耦合性大大降低,可以更加专注的在业务的实现上,这是IOC的原型
3、Hello Spring
<?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="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
控制反转:
控制:由谁来控制对象的创建,传统应用程序是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的
反转:程序本身不创建对象,而变成被动的接收对象
依赖注入:就是利用Set方法来进行注入的
IOC是一种编程思想,由主动的编程编程被动的接收
可以通过newClassPathXmlApplicationContext去浏览一下底层源码
4、IOC创建对象方式
1.使用无参构造创建对象,默认
2.假设我们要使用有参构造创建对象
- 下标赋值
<!--第一种,下标赋值-->
<bean id="user" class="com.kj.pojo.User">
<constructor-arg index="0" value="daxi"/>
</bean>
- 类型
<!--第二种,通过类型创建,不建议使用-->
<bean id="user" class="com.kj.pojo.User">
<constructor-arg type="java.lang.String" value="达西"/>
</bean>
- 参数名
<!--第三种,直接通过参数名来设置-->
<bean id="user" class="com.kj.pojo.User">
<constructor-arg name="name" value="伊丽莎白"/>
</bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了
5、配置
1、别名
<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="user2"/>
2、Bean的配置
<!--
id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的全限定名:包名+类型
name:也是别名,而且name可以同时取多个别名
-->
<bean id="user" class="com.kj.pojo.User" name="user2 u2,u3;u4">
<property name="name" value="daxi"/>
</bean>
3、import
一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的
- 张三
- 李四
- 王五
- applicationContext.xml
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
使用的时候,直接使用总的配置就行了
6、依赖注入
1、构造器注入
前面已经说过了
2、Set方式注入【重点】
- 依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
-
复杂类型
public class Address { private String address;
-
真实测试对象
public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; }
-
beans.xml
<bean id="student" class="com.kj.pojo.Student"> <!--第一种,普通值注入,使用value--> <property name="name" value="达西"/> </bean>
-
测试类
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.toString()); // Student{name='达西' // address=Address{address='上海'}, // books=[语文, 数学, 英语] // hobbies=[听歌, 画画, 写代码] // card={身份证=1111111111111111111, 银行卡=222222222222222222222} // games=[LOL, COC, BOB] // wife='null' // info={学号=1111, 姓名=张三}} } }
完善注入信息:
<bean id="address" class="com.kj.pojo.Address"> <property name="address" value="上海"/> </bean> <bean id="student" class="com.kj.pojo.Student"> <!--第一种,普通值注入,使用value--> <property name="name" value="达西"/> <!--第二种,Bean注入,ref--> <property name="address" ref="address"/> <!--第三种,数组--> <property name="books"> <array> <value>语文</value> <value>数学</value> <value>英语