Spring
Spring是什么?
-
Spring是一种轻量级语言.
-
它是为了解决java企业级开发而创建的.
-
核心是IoC(控制反转)和AOP(面向切面编程).
-
非侵入式的特点(Spring没有侵入到业务代码中)
-
针对接口编程,解耦合
-
一站式框架
Spring 体系结构:
Spring hello word搭建
Maven 导入 spring 核心基础 jar:
<!-- spring-context 自动加载spring核心功能所需的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
编写 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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
将java中的类交给spring管理 由spring容器管理对象.
-->
<bean id="user" class="com.ff.spring.bean.User" ></bean>
</beans>
写一个User类
测试Spring
package com.ff.spring.test;
import com.ff.spring.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
@Test
public void save(){
//加载spring配置文件,启动spring框架
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = (User)applicationContext.getBean("user");
System.out.println(user);
}
}
IOC(控制反转)
只是一种设计思想.就是将原本在程序中手动创建对象的控制权,交给Spring框架来管理.
IOC 容器是具有依赖注入功能的容器,负责对象的实例化、对象的初始化,对象和对象之间依赖关系配置、对象的销毁、对外提供对象的查找等操作,对象的整个生命周期都是由容器来控制。我们需要使用的对象都由 ioc 容器进行管理,不需要我们再去手动通过 new 的方式去创建对象,由 ioc 容器直接帮我们组装好,当我们需要使用的时候直接从 ioc 容器中直接获取就可以了。
正控:
若要使用哪个对象,需要手动自己负责创建.
反控:
若要使用哪个对象,只需要从 Spring 容器中获取需要使用的对象,不关心对象的创建过程,也就是把创建对象的控制权反转给了 Spring 框架.
目的:降低耦合度
底层实现方式:
解析 xml/扫描注解标签 + 工厂模式 + 反射机制
Spring bena管理
基于xml的配置方法:
bean:配置时需要Spring管理的类.
id:生成的对象名.
class:全类名
name:对象的别名,可以为多个.
scope:
singleton(默认值):在 Spring 中只存在一个 bean 实例, 单例模式. prototype:原型 getBean()的时候都会 new Bean()
request:每次 http 请求都会创建一个 bean, 仅用于 WebApplicationContext环境
session:同一个 http session 共享一个 Bean, 不同 Session 使用不同的 Bean, 使用环境同上.
Spring bean的作用域
scope="singleton" 单例的(单实例的) Spring框架在他的容器中只创建了一个类的对象
在Spring框架启动时就创建了,在Spring容器关闭时销毁
scope="prototype" 原型的(多例的) 每一次获得对象时(getBean()),Spring框架才会去创建对象
scope="request" 为每个http请求创建一个对象scope="session" 为每一次对话创建一个对象
scope="application" 为一个应用程序创建一个对象
Xml配置方式注入(DI:Dependency Injection):
指Spring创建对象时,将对象依赖属性(简单值,对象,集合)配置设置给该对象.
实现 IOC 需要 DI 做支持
注入的方式:
set方法注入:
<!--
创建对象的同时,让Spring为对象的属性赋值(注入值).
依赖注入的方式一:通过属性的set方法注入值
<property name="name" value="jim"></property>
-->
<bean id="user" class="com.ff.spring.bean.User" >
<property name="name" value="jim"></property>
<property name="age" value="20"></property>
<property name="list" >
<list>
<value>1</value>
<value>1</value>
<value>1</value>
</list>
</property>
<property name="map">
<map>
<entry key="k1" value="v1"></entry>
<entry key="k2" value="v2"></entry>
<entry key="k3" value="v3"></entry>
</map>
</property>
</bean>
构造方法注入:
<!-- 依赖注入方式二: 通过构造方法注入值-->
<bean class="com.ff.spring.bean.User" id="user"> <!-- 通过参数类型来检测使用哪个构造方法-->
<!--<constructor-arg name="name" value="sss"></constructor-arg>
<constructor-arg name="age" value="13"></constructor-arg>-->
<!--<constructor-arg index="0" value="法国红酒"></constructor-arg>
<constructor-arg index="1" value="13"></constructor-arg>-->
<constructor-arg type="java.lang.String" value="第三代"></constructor-arg>
<constructor-arg type="int" value="13"></constructor-arg>
</bean>
onstructor-arg>–>
<constructor-arg type="java.lang.String" value="第三代"></constructor-arg>
<constructor-arg type="int" value="13"></constructor-arg>
</bean>