1、Spring
1.1、简介
- 2002,首次推出了Spring框架的雏形:interface21框架!
- Spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于*2004年3月24日,*发布了1.0正式版。
- Rod Johnson ,Spring Framework创始人,悉尼大学的音乐学博士
- spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架!
现有的技术栈
- SSH : Struct2 + Spring + Hibernate!
- SSM : SpringMvc + Spring + Mybatis!
Spring的获取:
官网:https://spring.io/projects/spring-framework#overview
官方下载地址: http://repo.spring.io/release/org/springframework/spring
GitHub:https://github.com/spring-projects/spring-framework
需要导入的jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
1.2、优点
- Spring是一个开源的免费的框架(容器)!
- Spring是一个轻量级的、非入侵式的框架!
- 控制反转(IOC) , 面向切面编程(AOP)!
- 支持事务的处理,对框架整合的支持!
Spring就是一个轻量级的控制反转(IOC) 和面向切面编程(AOP)的框架!
弊端:发展了太久之后,违背了原来的理念!配置十分繁琐,人称:“ 配置地狱!”
1.3、组成
1.4、拓展
在Spring的官网有这个介绍:现代化的Java开发!说白就是基于Spring的开发!
- Spring Boot
- 一个快速开发的脚手架。
- 基于SpringBoot可以快速的开发单个微服务。
- 约定大于配置!
- Spring Cloud
- SpringCloud 是基于SpringBoot实现的。
2.IOC的本质
中文的意思为控制反转,也可以将他理解为一个容器。
- 是一种的设计的思想,就是将原本在程序中创建对象的控制权,交由Spring框架进行管理。
- 正常控制:new一个对象
- 反控:如果要使用某个对象,是需要只需要从Spring容器获取需要使用的对象,不用关心对象的创建的过程,也就是将创建对象的控制权交给Spring框架。
3.HelloSpring
- 导入Spring相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
- 编写相关代码
2.1编写一个Hello实体类
package com.jiao;
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello:"+name);
}
}
2.2 写编写我们的spring文件 , 这里我们命名为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就是java对象,由Spring创建和管理-->
<bean id="hello" class="com.jiao.Hello">
<property name="name" value="张阿娇"/>
</bean>
</beans>
2.3 我们可以去进行测试了 .
import com.jiao.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Test(){
//解析beans.xml文件,生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean:参数即为Spring配置文件中bean的id
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
}
4、IOC创建对象的方式
- 使用无参构造创建对象,默认!
- 假设我们要使用有参构造创建对象。
在beans.xml文件中
无参构造创建对象
方式1:下标赋值
<bean id="user" class="com.jiao.pojo.User">
<constructor-arg index="0" value="张阿娇"></constructor-arg>
</bean>
方式2:直接通过参数名来赋值
<bean id="user" class="com.jiao.pojo.User">
<constructor-arg name="name" value="南胖子"/>
</bean>
方式3:通过类型创建,不建议使用
<bean id="user" class="com.jiao.pojo.User">
<constructor-arg type="java.lang.String" value="加油啊"/>
</bean>
5、Spring配置
5.1、别名
<bean id="UserTwo" class="com.jiao.pojo.UserTwo">
<constructor-arg index="0" value="张可爱"/>
</bean>
别名:如果添加了别名,我们也可以使用别名获取到这个对象
<alias name="UserTwo" alias="user"/>
name也是别名,而且name可以同时取多个别名
<bean id="UserTwo" class="com.jiao.pojo.UserTwo" name="u1,u2,u3">
<constructor-arg type="java.lang.String" value="南狗"/>
</bean>
5.2、import
这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个。假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的applicationContext.xml!
<import resource="beans.xml"/>
<import resource="beans1.xml"/>
6、依赖注入
6.1、构造器注入
6.2、Set方式注入 【重点】
- 依赖注入:Set注入!
依赖:bean对象的创建依赖于容器!
注入: bean对象中的所有属性,由容器来注入!
1.Address类
package com.jiao.pojo;
public class Address {
private String Address;
2.Student类
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
private Set<String> games;
private String wife;
private Properties info;
3.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="com.jiao.pojo.Address">
<property name="address" value="陕西西安"/>
</bean>
<bean id="student" class="com.jiao.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="张可爱"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>西游记</value>
<value>三国演义</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>吃</value>
<value>喝</value>
<value>玩</value>
<value>乐</value>
</list>
</property>
<!--Map-->
<property name="cards">
<map>
<entry key="身份证" value="610424"/>
<entry key="银行卡" value="0077"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>AOA</value>
<value>BOB</value>
<value>COC</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">20150615140</prop>
<prop key="性别">女</prop>
</props>
</property>
</bean>
</beans>
4.测试类
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
6.3、拓展方式注入(p和c注入)
我们可以使用 p命令空间和c命令空间进行注入。
p命名空间注入,可以直接注入属性的值:property
<bean id="user" class="com.jiao.pojo.User" p:name="张可爱" p:age="11"/>
c命名空间注入,通过构造器注入:construct-args
<bean id="user" class="com.jiao.pojo.User" c:name="南胖子" c:age="20"/>
注意点:p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
6.4、bean的作用域
- 单例模式:Spring默认机制
<bean id="user" class="com.jiao.pojo.User" c:name="张三" c:age="11" scope="singleton"/>
- 原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="user" class="com.jiao.pojo.User" c:name="李四" c:age="23" scope="prototype"/>