视频链接:https://www.bilibili.com/video/BV1WE411d7Dv
文章目录
1、Spring
1.1、简介
spring官网: https://spring.io/projects/spring-framework#overview
官方下载: https://repo.spring.io/release/org/springframework/spring/
GitHub: https://github.com/spring-projects/spring-framework
Spring Web MVC: spring-webmvc最新版
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
1.2、优点
- Spring是一个开源的免费框架(容器)!
- Spring是一个轻量级的非入侵式的框架
- 控制反转(IOC),面向切面编程(AOP)!
- 支持事务的处理,对框架整合的支持
开源免费容器,轻量级非侵入式,控制反转,面向切面,支持事务,支持框架整合
Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)编程的框架
1.3、组成
1.4、扩展
现代化的java开发 -> 基于Spring的开发
2、IOC理论推导
传统的调用
-
UserDao
package dao; public interface UserDao { void getUser(); }
-
UserDaoImp
package dao; public class UserDaoImpl implements UserDao{ public void getUser() { System.out.println("默认获取用户数据"); } }
-
UserSevice
package Service; public interface UserService { void getUser(); }
-
UserServiceImp
package Service; import dao.UserDao; import dao.UserDaoImpl; public class UserServiceImpl implements UserService{ UserDao userDao = new UserDaoImpl(); public void getUser(){ userDao.getUser(); } }
测试
package holle0;
import Service.UserService;
import Service.UserServiceImpl;
public class MyTest0 {
public static void main(String[] args) {
// 用户实际调用的是业务层,dao层他们不需要接触
UserService userService = new UserServiceImpl();
userService.getUser();
}
}
在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原代码!如果程序代码量十分大,修改一次的成本代价十分昂贵!
改良:我们使用一个Set接口实现。已经发生了革命性的变化!
//在Service层的实现类(UserServiceImpl)增加一个Set()方法
//利用set动态实现值的注入!
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
set() 方法实际上是动态改变了 UserDao userDao 的 初始化部分(new UserDaoImpl())
测试中加上
((UserServiceImpl)userService).setUserDao(new UserDaoImpl());
- 之前,程序是主动创建对象!控制权在程序猿手上!
- 使用了set注入后,程序不再具有主动性,而是变成了被动的接受对象!(主动权在客户手上)
本质上解决了问题,程序员不用再去管理对象的创建
系统的耦合性大大降低,可以更专注在业务的实现上
这是IOC(控制反转)的原型,反转(理解):主动权交给了用户
IOC本质
3、HolleSpring
在父模块中导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
pojo的Hello.java
package pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Holle [str=" + str + "]";
}
}
在resource里面的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">
<!--在Spring中创建对象,在Spring这些都称为bean
类型 变量名 = new 类型();
Holle holle = new Holle();
bean = 对象(holle)
id = 变量名(holle)
class = new的对象(new Holle();)
property 相当于给对象中的属性设值,让str="Spring"
-->
<bean id="hello" class="pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
测试类MyTest
package holle1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Hello;
public class MyTest {
public static void main(String[] args) {
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象下能在都在spring·中管理了,我们要使用,直接取出来就可以了
Hello holle = (Hello) context.getBean("hello");
System.out.println(holle.toString());
}
}
核心用set注入,所以必须要有下面的se()方法
//Hello类
public void setStr(String str) {
this.str = str;
}
思考:
IOC:对象由Spring 来创建,管理,装配!
弹幕评论里面的理解:
原来这套程序是:你写好菜单买好菜,客人来了自己把菜炒好招待,就相当于你请人吃饭
现在这套程序是:你告诉楼下餐厅,你要哪些菜,客人来的时候,餐厅把做好的你需要的菜送上来
IoC:炒菜这件事,不再由你自己来做,而是委托给了第三方__餐厅来做
此时的区别就是,如果我还需要做其他的菜,我不需要自己搞菜谱买材料再做好,而是告诉餐厅,我要什么菜,什么时候要,你做好送来
.
在前面第一个module试试引入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="userDaomSql" class="dao.UserDaoMysqlImpl"></bean>
<bean id="userServiceImpl" class="service.UserServiceImp">
<!--ref引用spring中已经创建很好的对象-->
<!--value是一个具体的值,基本数据类型-->
<property name="userDao" ref="userDaomSql"/>
</bean>
</beans>
第一个module改良后测试
package holle0;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserServiceImpl;
public class MyTest0 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
userServiceImpl.getUser();
}
}
总结:
所有的类都要装配的beans.xml 里面;
所有的bean 都要通过容器去取;
容器里面取得的bean,拿出来就是一个对象,用对象调用方法即可;
4、IOC创建对象的方式
- 使用无参构造创建对象,默认。
- 使用有参构造(如下)
下标赋值
index指的是有参构造中参数的下标,下标从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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="pojo.User">
<constructor-arg index="0" value="chen"/>
</bean>
</beans>
类型赋值(不建议使用)
<bean id="user" class="pojo.User">
<constructor-arg type="java.lang.String" value="kuang"/>
</bean>
直接通过参数名(掌握)
<bean id="user" class="pojo.User">
<constructor-arg name="name" value="kuang"></constructor-arg>
</bean>
<!-- 比如参数名是name,则有name="具体值" -->
注册bean之后就对象的初始化了(类似 new 类名())
弹幕评论:
name方式还需要无参构造和set方法,index和type只需要有参构造
就算是new 两个对象,也是只有一个实例(单例模式:全局唯一)
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
system.out.println(user == user2)//结果为true
总结:在配置文件加载的时候,容器(< bean>)中管理的对象就已经初始化了
5、Spring配置
5.1、别名
<bean id="user" class="pojo.User">
<constructor-arg name="name" value="chen"></constructor-arg>
</bean>
<alias name="user" alias="userLove"/>
<!-- 使用时
User user2 = (User) context.getBean("userLove");
-->
5.2、Bean的配置
<!--id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的会限定名:包名+类型
name:也是别名,而且name可以同时取多个别名 -->
<bean id="user" class="pojo.User" name="u1 u2,u3;u4">
<property name="name" value="chen"/>
</bean>
<!-- 使用时
User user2 = (User) context.getBean("u1");
-->
5.3、import
import一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以利
用import将所有人的beans.xml合并为一个总的!
-
张三(beans.xm1)
-
李四(beans2.xm1)
-
王五(beans3.xm1)
-
applicationContext.xml
<import resource="beans.xm1"/> <import resource="beans2.xml"/> <import resource="beans3.xm1"/>
使用的时候,直接使用总的配置就可以了
弹幕评论:
按照在总的xml中的导入顺序来进行创建,后导入的会重写先导入的,最终实例化的对象会是后导入xml中的那个
6、依赖注入(DI)
6.1、构造器注入
第4点有提到
6.2、set方式注入【重点】
依赖注入:set注入!
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
-
复杂类型
Address类
-
真实测试对象
Student类
-
beans.xml
-
测试
MyTest3
Student类
package pojo;
import java.util.*;
@Get
@Set
public class Student {
//别忘了写get和set方法(用lombok注解也行)
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> game;
private Properties infor;
private String wife;
@Override
public String toString() {
return "Student{" +"\n"+
"name='" + name + '\'' +"\n"+
", address=" + address.toString() +"\n"+
", books=" + Arrays.toString(books) +"\n"+
", hobbies=" + hobbies +"\n"+
", card=" + card +"\n"+
", game=" + game +"\n"+
", infor=" + infor +"\n"+
", wife='" + wife + '\'' +"\n"+
'}';
}
}
Address类
package pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
beans.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="pojo.Address">
<property name="address" value="address你好" />
</bean>
<bean id="student" class="pojo.Student">
<!--第一种,普通值注入 -->
<property name="name" value="name你好" />
<!--第二种,ref注入 -->
<property name="address" ref="address" />
<!--数组注入 -->
<property name="books">
<array>
<value>三国</value>
<value>西游</value>
<value>水浒</value>
</array>
</property>
<!--list列表注入 -->
<property name="hobbies">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
<!--map键值对注入 -->
<property name="card">
<map>
<entry key="username" value="root" />
<entry key="password" value="root" />
</map>
</property>
<!--set(可去重)注入 -->