一.概述
1.简介
2002年,首次推出了Spring框架的雏形----interface21框架。
2004年3月24日,以interface21框架为基础正式发布了Spring1.0正式版,它是一个轻量级的Java 开发框架,由Rod Johnson创建。
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
官网:https://spring.io/projects/spring-framework#overview
官方下载地址: http://repo.spring.io/release/org/springframework/spring
导入的依赖
<!-- 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>
2.优点
- Spring是一个开源的免费的框架(容器)!
- Spring是一个轻量级的、非入侵式的框架!
- 控制反转(IOC) , 面向切面编程(AOP)!
- 支持事务的处理,对框架整合的支持!
最终定义:
Spring就是一个轻量级的控制反转(IOC) 和面向切面编程(AOP)的框架!
3.组成
二.控制反转(IoC)
1.概述
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法
- 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的 .
- 反转 : 程序本身不创建对象 , 而变成被动的接收对象
- 依赖注入 : 就是利用set方法来进行注入的
在没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制
控制反转后将对象的创建转移给第三方,对象由Spring 来创建 , 管理 , 装配
个人认为所谓控制反转就是:获得依赖对象的方式反转了。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。
2.第一个Spring程序
(1)导入Spring相关jar包
注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
(2)编写一个Hello实体类
package com.yl.pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public void show(){
System.out.println("Hello "+str+"!");
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
(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="hello" class="com.yl.pojo.Hello">
<property name="str" value="String" />
</bean>
</beans>
(4)编写测试文件
@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();
System.out.println(hello.toString());
}
3.IOC创建对象的方式
默认使用无参构造
(1)使用有参构造创建的方法
下标法创建法:
<bean id="user" class="com.yl.pojo.User">
<constructor-arg index="0" value="张三"/>
</bean>
类型创建法(不建议使用)
<bean id="user" class="com.yl.pojo.User">
<constructor-arg type="java.lang.String" value="张三"/>
</bean>
参数名创建法
<bean id="user" class="com.yl.pojo.User">
<constructor-arg name="name" value="张三"/>
</bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!
三.Spring配置
1.别名
<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="userNew"/>
2.Bean的配置
<!--
id : bean 的唯一标识符,也就是相当于我们学的对象名
class : bean 对象所对应的全限定名 : 包名 + 类型
name :也是别名,而且name 可以同时取多个别名,中间的分隔符可以是空格、逗号、分号等
-->
<bean id="user" class="com.yl.pojo.User" name="user2 user3;user4,user5">
<property name="name" value="张三" />
</bean>
3.import
import一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
(1)建立总的合并配置文件applicationContext.xml
合并beans.xml,beans2.xml,beans3.xml配置文件代码如下
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
四.依赖注入
1.Set方式注入 【重点】
复杂类型
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 + '\'' +
'}';
}
}
真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.yl.pojo.Address" />
<bean id="student" class="com.yl.pojo.Student">
<property name="name" value="张三" />
<property name="address" ref="address" />
<property name="books">
<array>
<value>西游</value>
<value>三国</value>
</array>
</property>
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="666666666666666666" />
<entry key="银行卡" value="111111111111111111" />
</map>
</property>
<property name="games">
<set>
<value>LoL</value>
<value>CS</value>
<value>DNF</value>
</set>
</property>
<property name="wife">
<null />
</property>
<property name="info">
<props>
<prop key="username">zhangsan</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="singleton"/>
</beans>
测试类
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);
System.out.println(student.getAddress());
}
}
2.拓展方式注入
可以使用 p命令空间和c命令空间进行注入
(1)建立实体类并重写相关方法
package com.yl.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2)建立beans.xml并导入xml约束
xml约束
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="singleton"/>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.yl.pojo.User" c:name="li" c:age="18" scope="singleton" />
</beans>
(3)编写测试文件
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
User user2 = context.getBean("user", User.class);
System.out.println(user==user2);
}
(4)bean的作用域(scope)
单例模式 (Spring默认机制)
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="singleton"/>
原型模式
每次从容器中get的时候,都会产生一个新对象!
<bean id="user" class="com.yl.pojo.User" p:name="zhang" p:age="12" scope="prototype"/>