一、Spring 概述
1、简介
Spring是一个开源的框架,Spring 为简化企业级开发而生,使用 Spring、JavaBean 就可以实现很多以前要靠 EJB 才能实现的功能,同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能够实现,而使用Spring 却非常的优雅和简洁。
Spring 的核心:
IOC(Inversion of Control):控制反转,即对象创建的问题
AOP(Aspect Oriented Programming):面向切面编程
IOC:控制反转,把传统上由程序代码直接操控的对象的调用权交给容器,通过容器来实现组件的装配和管理,本质就是对象的调用权的转移,将创建对象的权利交给了容器
DI:依赖注入,在运行期,由外部容器动态地将依赖对象注入到另一个对象中
IOC描述的是一种思想,DI是对IOC思想的具体实现
Spring 优点
高内聚,低耦合
Spring 就是一个大工厂,可以将所有对象创建和依赖关系维护都交给Spring来管理
AOP 编程的支持
Spring 提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
方便程序的测试
Spring 对 Junit4支持,可以通过注解来测试 Spring 程序
2、搭建 Spring IOC环境
创建 maven 版的 Java 工程
导入 jar 包
在pom.xml中加入相关依赖,导入Spring相关jar包(为了方便后面进行测试,这里导入了测试jar包)
<packaging>jar</packaging>
<dependencies>
<!--导入Spring相关jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--导入测试相关jar包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
创建 Spring 配置文件
在 resources 目录下创建配置文件,内容如下(可以从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">
</beans>
创建实体类
创建一个简单的HelloWorld类
public class Helloworld {
private String name;
public Helloworld(){
System.out.println("创建对象");
}
public void setName(String name) {
System.out.println("调用方法");
this.name = name;
}
public void sayHello(){
System.out.println("Hello!" + name);
}
}
对类进行配置
配置之后如下
<?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">
<!--配置bean
id属性:给当前bean起一个名称,该名称必须保证是唯一的
class属性:设置bean的全类名-->
<bean id="helloworld" class="com.LSTAR.Helloworld">
<!--给属性赋值
name属性:设置bean属性名
value属性:设置bean属性值-->
<property name="name" value="LSTAR"></property>
</bean>
</beans>
【6】测试类
public class test {
@Test
public void test(){
//1.创建IOC容器对象
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Helloworld对象
Helloworld hello = (Helloworld) ioc.getBean("helloworld");
//3.调用类中方法
hello.sayHello();
}
}
二、Spring对Bean的管理
1、创建bean的方式
默认使用构造函数创建
在Spring的配置文件中使用bean标签,配置 id 和 class 属性后,没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
<bean id="helloworld" class="com.LSTAR.Helloworld">
类中的方法创建
很多时候我们会使用某个类中的或者jar包中的方法或者普通工厂中的方法来创建对象,并存入到Spring容器中
<bean id="school" class="com.LSTAR.School"></bean>
<bean id="helloworld" factory-bean="school" factory-method="getHelloworld"></bean>
类中的静态方法创建
使用某个类中的静态方法创建或者工厂中的或者jar包中的静态方法创建,并存入到Spring容器中
<bean id="school" class="com.LSTAR.School"></bean>
<bean id="helloworld" class="com.LSTAR.School" factory-method="getHelloworld"></bean>
2、获取bean的方式
配置文件就是让IOC容器能够来管理一个具体的对象,主要就是通过配置<bean>进行管理,Spring 能够通过获取<bean>的配置来获取到对应的对象以及属性,<bean>中 id 表示唯一属性,class 表示类的全类名,通过反射的方式创建对象:
Class class = Class.forName("com.LSTAR.Helloworld");
Object obj = class.=newlnstance(); //无参构造函数
获取bean有三种方式
根据 id 获取
通过唯一标识 id 来获取,上面的环境搭建就是用的这种方式
Helloworld hello = (Helloworld) ioc.getBean("helloworld");
通过 bean 类型获取
如果同一个类型的bean在xml文件中配置了多个,则获取时会抛出异常,所以同一个类型的bean在容器中必须是唯一的
Helloworld hello = ioc.getBean(Helloworld.class);
指定bean的 id 值和类型
Helloworld hello = ioc.getBean("helloworld",Helloworld.class);
3、bean 对象的生命周期
单例对象
出生:当容器创建的时候bean对象出生
活着:只要容器还在,bean对象就一直活着
死亡:容器销毁,bean对象就消亡
单例bean对象随容器共存亡
多例对象
出生:当使用对象时,Spring创建对象
活着:对象只要在使用过程中就一直活着
死亡:当对象长时间不用且没有其他对象引用时,有Java垃圾回收器回收
4、bean 的作用范围
通过bean 标签的 scope 属性来指定 bean 的作用范围,属性值有:
singleton:单例的(默认值)
prototype:多例的
request:作用于 web 应用的请求范围
session:作用于 web 应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,就是session