spring介绍
1. spring概述
1.1 Spring是什么
Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核。
提供了展现层 SpringMVC和持久层 Spring JDBCTemplate以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架
1.2 Spring发展历程
Rod Johnson ( Spring 之父)
Spring的基础架构起源于2000年早期,它是Rod Johnson在一些成功的商业项目中构建的基础设施
2002后期,Rod Johnson发布了《Expert One-on-One J2EE Design and Development》一书
2003年2月Spring框架正式成为一个开源项目,并发布于SourceForge中
1.3 Spring的优势
方便解耦,简化开发
AOP 编程的支持
声明式事务的支持
方便程序的测试
1.4 Spring的体系结构
spring IOC底层原理:
工厂模式、配置文件、反射
1、创建交由工厂管理的类
public class Class1 {
public Class1() {
System.out.println("class1创建");
}
}
public class Class2 {
public Class2() {
System.out.println("class2创建");
}
}
2、创建工厂类
import java.util.HashMap;
//类工厂
public class ClassFactory {
//声明保存配置信息的map集合
private static HashMap<String, String> hashMap=new HashMap<>();
static {
// 模拟加载配置文件
hashMap.put("c1", "com.yunhe.pojo.Class1");
hashMap.put("c2", "com.yunhe.pojo.Class2");
}
// 传入配置名字获取指定对象返回
public Object getClass(String name) {
//查找配置文件获取对应地址
String string = hashMap.get(name);
if(string==null) {
System.out.println("请输入正确的名字");
return null;
}else {
//通过反射获取指定class对象的类对象
Class<?> forName;
try {
forName = Class.forName(string);
} catch (ClassNotFoundException e) {
System.out.println("名字对应对象找不到");
return null;
}
//调用对象无参构造方法创建对象
Object newInstance = null;
try {
newInstance = forName.newInstance();
} catch (Exception e) {
System.out.println("调用无参构造方法创建对象失败");
return null;
}
return newInstance;
}
}
}
3、创建测试类
public class Test {
public static void main(String[] args) {
//获取工厂对象
ClassFactory cf=new ClassFactory();
//输入配置的name获取对应类对象
cf.getClass("c2");
}
}
2. 简单spring快速构建
2.1 Spring程序开发步骤
①导入 Spring 开发的基本包坐标
<!--导入spring的context坐标,context依赖core、beans、expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
注意:Spring使用导入的spring-context 的jar包依赖于 spring-core、spring-beans、spring-experssion但使用maven工程会自动根据spring的配置文件导入相应的依赖jar包
②编写 接口和实现类
//服务层接口
public interface UserService {
public void save();
}
//服务层实现类-》交由spring进行管理的类
import com.yh.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("保存执行~~~~~~~~~~");
}
}
③创建 Spring 核心配置文件
在resources文件中(maven项目中这个文件夹相当于src)
在导入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>
④在 Spring 配置文件中配置交由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">
<bean id="userService" class="com.yh.service.impl.UserServiceImpl"></bean>
</beans>
书写格式:
<bean id="唯一标识符" class="交由spring管理的类的全路径"></bean>
<!--api的getbean就是通过id或class进行获取指定实例-->
⑤使用 Spring 的 API 获得 Bean 实例
//使用spring对指定类进行管理后获取指定对象
//通过配置 将对象设置的名字与全路径名进行配置
//使用相应工具类进行加载配置文件获取交由spring管理的所有类的信息
//通过getBean 传入要获取对象的对应的id获取指定对象
//强转为需要使用的对象 调用相应方法
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("spring.xml");
//通过唯一标识符id进行获取
UserService UserService1=(UserService) applicationContext.getBean("userService");
//通过类class进行自动匹配
UserService UserService2=(UserService) applicationContext.getBean(UserServiceImpl.class);
ApplicationContext的继承体系
applicationContext:接口类型,代表应用上下文,可以通过其实例获得 Spring 容器中的 Bean 对象
ApplicationContext的实现类
1)ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件 推荐使用这种(直接获取指定名字xml文件)
2)FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。(输入配置文件全路径获取)
3)AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。
getBean()方法使用(源码)
public Object getBean(String name) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name);
}
public <T> T getBean(Class<T> requiredType) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(requiredType);
}
其中,当参数的数据类型是字符串时,表示根据Bean的id从容器中获得Bean实例,返回是Object,需要强转。
当参数的数据类型是Class类型时,表示根据类型从容器中匹配Bean实例,当容器中相同类型的Bean有多个时,则此方法会报错
3.spring核心配置文件的书写
3.1 Bean标签基本配置
用于配置对象交由Spring 来创建。
默认情况下它调用的是类中的无参构造函数,如果没有无参构造函数则不能创建成功。
基本属性:
id:Bean实例在Spring容器中的唯一标识
class:Bean的全限定名称
<bean id="唯一标识符" class="交由spring管理的类的全路径"></bean>
<!--api的getbean就是通过id或class进行获取指定实例-->
3.2 Bean标签scope范围属性配置
scope:指对象的作用范围,取值如下:
取值范围 | 说明 |
---|---|
singleton | 默认值,单例的 |
prototype | 多例的 |
1)当scope的取值为singleton时
Bean的实例化个数:1个
Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
Bean的生命周期:
对象创建:当应用加载,创建容器时,对象就被创建了
对象运行:只要容器在,对象一直活着
对象销毁:当应用卸载,销毁容器时,对象就被销毁了
2)当scope的取值为prototype时
Bean的实例化个数:多个
Bean的实例化时机:当调用getBean()方法时实例化Bean
对象创建:当使用对象时,创建新的对象实例
对象运行:只要对象在使用中,就一直活着
对象销毁:当对象长时间不用时,被 Java 的垃圾回收器回收了
<bean id="userService" class="com.yh.service.impl.UserServiceImpl" scope="singleton" ></bean>
3.3 Bean生命周期配置
1)需要在指定类中存在相应方法
2)在配置文件中进行调用
在bean标签中配置指定类初始化方法与销毁方法并在生命周期中自动调用
init-method:指定类中的初始化方法名称
destroy-method:指定类中销毁方法名称
<bean id="userService" class="com.yh.service.impl.UserServiceImpl" init-method="init" destroy-method="destroy" ></bean>
AbstractApplicationContext applicationContext = new
ClassPathXmlApplicationContext("spring.xml");
//在getbean创建对象之后会自动调用初始化方法
UserService UserService1 = (UserService) applicationContext.getBean("userService");
//单例创建的bean会随着spring的关闭自动调用销毁方法
applicationContext.close();
3.4 Bean实例化三种方式
1) 使用无参构造方法实例化
它会根据默认无参构造方法来创建类对象,如果bean中没有默认无参构造函数,将会创建失败
<bean id="UserService" class="com.yunhe.dao.impl.UserServiceImpl"></bean>
2) 工厂静态方法实例化
工厂的静态方法返回Bean实例
public class StaticFactoryBean {
public static UserService createUserService(){
return new UserServiceImpl();
}
}
<bean id="UserService" class="com.yunhe.factory.StaticFactoryBean"
factory-method="createUserService" />
3) 工厂实例方法实例化
工厂的非静态方法返回Bean实例
public class DynamicFactoryBean {
public UserService createUserService(){
return new UserServiceImpl();
}
}
<bean id="factoryBean" class="com.yh.factory.DynamicFactoryBean"/>
<bean id="userDao" factory-bean="factoryBean" factory-method="createUserDao"/>
3.5 Spring IOC/DI
IOC控制反转Inversion of Control
把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。
DI依赖注入Dependency Injection
在程序运行期间由容器动态的将某个依赖关系注入到组件之中
简单可以理解为:
ioc负责对象在容器的创建销毁初始化,di负责存值
3.5.1 Bean的依赖注入方式
Student.java
public class Student implements Serializable {
private String name;
private int age;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Teacher.java
public class Teacher implements Serializable {
private String name;
private Student stu;
public Teacher(String name, Student stu) {
this.name = name;
this.stu = stu;
}
public Teacher() {
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", stu=" + stu +
'}';
}
public void setName(String name) {
this.name = name;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
}
①构造方法注入
1)普通属性注入
<!--构造方法注入普通属性-->
<bean id="student1" class="com.yh.pojo.Student">
<constructor-arg name="name" value="张三"/>
<constructor-arg name="age" value="18"/>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Student student1 = (Student) ac.getBean("student1");
System.out.println(student1);
2)类属性注入
通过ref将spring管理的另一个类对象注入(先将注入的属性在spring进行创建)
<!--构造方法注入普通属性-->
<bean id="student1" class="com.yh.pojo.Student">
<constructor-arg name="name" value="张三"/>
<constructor-arg name="age" value="18"/>
</bean>
<!--构造方法注入类属性-->
<bean id="teacher1" class="com.yh.pojo.Teacher">
<constructor-arg name="name" value="帅哥"/>
<constructor-arg name="stu" ref="student1"/>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Teacher teacher1 = (Teacher) ac.getBean("teacher1");
System.out.println(teacher1);
②set方法
1)普通属性注入
<!-- set方法注入普通属性 -->
<bean id="student2" class="com.yh.pojo.Student">
<property name="name" value="李四"/>
<property name="age" value="19"/>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Student student2 = (Student) ac.getBean("student2");
System.out.println(student2);
2)类属性注入
通过ref将spring管理的另一个类对象注入(先将注入的属性在spring进行创建)
<!-- set方法注入普通属性 -->
<bean id="student2" class="com.yh.pojo.Student">
<property name="name" value="李四"/>
<property name="age" value="19"/>
</bean>
<!--set方法注入类属性-->
<bean id="teacher2" class="com.yh.pojo.Teacher">
<property name="name" value="大帅哥"/>
<property name="stu" ref="student2"/>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Teacher teacher2 = (Teacher) ac.getBean("teacher2");
System.out.println(teacher2);
③自动注入(接口注入、注解注入)
开启注解开发后书写注解自动进行注入操作
集合数据的注入
①常用数据集合注入(基本数据类型、String)
Boss.java
public class Boss {
private List<String> teachers=new ArrayList<>();
public void setTeachers(List<String> teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Boss{" +
"teachers=" + teachers +
'}';
}
}
<!-- 注入List集合 -->
<bean id="boss1" class="com.yh.pojo.Boss">
<property name="teachers">
<list>
<!-- set方法注入 list代表注入参数为List集合 如果参数为arralist会报错 -->
<!-- 但是不影响注入 -->
<value>张三</value>
<value>李思思</value>
</list>
</property>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Boss boss1 = (Boss) ac.getBean("boss1");
System.out.println(boss1);
②引用数据类型集合注入(自定义类型)
Boss.java
public class Boss {
private List<Teacher> teachers=new ArrayList<>();
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Boss{" +
"teachers=" + teachers +
'}';
}
}
<!-- set方法注入普通属性 -->
<bean id="student2" class="com.yh.pojo.Student">
<property name="name" value="李四"/>
<property name="age" value="19"/>
</bean>
<!--set方法注入类属性-->
<bean id="teacher2" class="com.yh.pojo.Teacher">
<property name="name" value="大帅哥"/>
<property name="stu" ref="student2"/>
</bean>
<!--注入List集合-->
<bean id="boss2" class="com.yh.pojo.Boss">
<property name="teachers">
<list>
<!--方式1 直接创建指定对象赋值-->
<bean class="com.yh.pojo.Teacher">
<property name="stu" ref="student1"/>
<property name="name" value="测试"/>
</bean>
<!-- 方式2 使用已经创建好的对象赋值-->
<ref bean="teacher2"/>
</list>
</property>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Boss boss2 = (Boss) ac.getBean("boss2");
System.out.println(boss2);
③map集合注入
Account.java
public class Account {
private Map<String,String> maps=new HashMap<>();
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
@Override
public String toString() {
return "Account{" +
"maps=" + maps +
'}';
}
}
<!-- map注入 -->
<bean id="account1" class="com.yh.pojo.Account">
<property name="maps">
<map>
<entry key="admin" value="password"/>
<entry key="zhagnsan" value="123456"/>
</map>
</property>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
Account account1 = (Account) ac.getBean("account1");
System.out.println(account1);
④properties注入
为properties类进行赋值
PropertiesT.java
public class PropertiesT {
private Properties p;
public void setP(Properties p) {
this.p = p;
}
@Override
public String toString() {
return "PropertiesT{" +
"p=" + p +
'}';
}
}
<!-- properties注入 -->
<bean id="p" class="com.yh.pojo.PropertiesT">
<property name="p">
<props>
<prop key="uesrname">root</prop>
<prop key="password">root</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
PropertiesT p = (PropertiesT) ac.getBean("p");
System.out.println(p);
@Override
public String toString() {
return "PropertiesT{" +
"p=" + p +
'}';
}
```xml
<!-- properties注入 -->
<bean id="p" class="com.yh.pojo.PropertiesT">
<property name="p">
<props>
<prop key="uesrname">root</prop>
<prop key="password">root</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
PropertiesT p = (PropertiesT) ac.getBean("p");
System.out.println(p);