说明 :IOC部分:参考网上课程,自己写的总结,如有侵权,联系删除。
目录
spring简介
Spring是一个开源框架,为简化企业级开发而生。它以IOC(控制 反转)和AOP(面向切面)为思想内核,提供了控制层 SpringMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。
Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
Spring官网地址:https://spring.io/
spring框架体系架构
Spring框架根据不同的功能被划分成了多个模块:
Core Container:Spring 的核心模块,任何功能使用都离不开这个模块,其他模块建立的基础
Data Access/Integration: 该模块提供了数据持久化的相应功能
web: web开发响应功能
AOP: 提供了面向切面编程实现
Aspects: 提供与AspectJ框架的集成,该框架是面向切面编程框架
Instrumentation: 提供了类工具的集成和类加载器的实现,可以在特定应用服务器使用
Messaging: 为spring框架集成一些基础的报文传送应用
Test: 提供与测试框架的集成。
IOC_控制反转思想
IOC(Inversion of Control) :程序将创建对象的权利交给框架管理,之前开发的时候,对象实例的创建由调用者管理。
之前写法的缺点:
1.浪费资源:每次Service调用方法的时候就会创建对象,一直调用会创建大量的dao层对象
2.代码耦合度高:假如创建了StudentDao另一个更加完善的实现类StudentDaoImpl2,想在Service层使用StudentDaoImpl2,必须改源代码
而IOC思想是将创建对象的权力交给框架,框架会帮助我们创建对象,分配对象的使用,控制权由程序代码转移到了框架中,控制权发生了反转,这就是Spring的IOC思想。IOC可以完美的解决以上两个问题。
IOC_自定义对象容器
通过自定义容器来模拟IOC思想。创建一个集合容器,先将对象创建出来放到容器,需要使用对象时,只需要从容器中获取对象即可,而不需要重新创建,此时容器就是对象的管理者。
- 创建实体类
public class Student { private int id; private String name; private String address; // 省略getter/setter/构造方法/tostring }
- 创建Dao接口和实现类
public interface StudentDao { // 根据id查询学生 Student findById(int id); } //实现类 public class StudentDaoImpl implements StudentDao{ @Override public Student findById(int id) { // 模拟从数据库查找出学生 return new Student(1001,"xxs","北京"); } } public class StudentDaoImpl2 implements StudentDao{ @Override public Student findById(int id) { // 模拟根据id查询学生 System.out.println("实现类2方法!!!"); return new Student(1002,"dds","天津"); } }
- 创建配置文件bean.properties,该文件中定义管理的对象
studentDao=com.dream.dao.StudentDaoImpl
- 创建容器管理类,该类在类加载时读取配置文件,将配置文件中配置的对象全部创建并放入容器中。
/** * 自定义容器类,负责管理对象,在类加载的时候读取配置文件并创建对象 */ public class Container { static Map<String,Object> map=new HashMap<>(); static { //读取配置文件 InputStream is = Container.class.getClassLoader().getResourceAsStream("bean.properties"); Properties properties=new Properties(); try { properties.load(is); } catch (Exception e) { e.printStackTrace(); } //遍历配置文件中的所有对象 Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()){ String key = keys.nextElement().toString(); String value=properties.getProperty(key); //创建对象 try { Object obj=Class.forName(value).newInstance(); map.put(key,obj); } catch (Exception e) { e.printStackTrace(); } } } //从容器中获取对象 public static Object getBean(String key){ return map.get(key); } }
- 创建Dao对象的调用者StudentService
public class StudentService { public Student findStudentById(int id){ // 从容器中获取对象 StudentDao studentDao =(StudentDao)Container.getBean("studentDao"); System.out.println(studentDao.hashCode()); return studentDao.findById(id); } }
这样就通过Container类读取配置文件获得studentDao对象,改对象的时候也方便,直接更改配置文件。这个就类似Spring容器的管理。
IOC_Spring 实现IOC
- 创建Maven工程,引入依赖
<dependencies> <!--spring 核心模块--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.21</version> </dependency> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
- 创建POJO类、Dao类和接口
public class Student { private int id; private String name; private String address; // 省略getter/setter/构造方法/tostring } public interface StudentDao { // 根据id查询学生 Student findById(int id); } public class StudentDaoImpl implements StudentDao{ @Override public Student findById(int id) { // 模拟从数据库查找出学生 return new Student(1001,"xxs","北京"); } }
-
编写bean.xml配置文件,配置文件中配置需要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="studentDao" class="com.dream.dao.StudentDaoImpl"></bean> </beans>
-
测试从Spring容器中获取对象。
public class TestSpring { @Test public void t1(){ //1.创建spring容器 ApplicationContext ac =new ClassPathXmlApplicationContext("bean.xml"); //2.从容器获取对象 StudentDao studentDao1 = (StudentDao) ac.getBean("studentDao"); StudentDao studentDao2 = (StudentDao) ac.getBean("studentDao"); System.out.println(studentDao1.hashCode()); System.out.println(studentDao2.hashCode());//这个是一样的 System.out.println(studentDao1.findById(1)); } }
IOC_Spring容器类型
容器接口:
BeanFactory:BeanFactory是Spring容器中的顶层接口,可以对Bean对象进行管理。
ApplicationContext:(我们常用的)ApplicationContext是BeanFactory的子接口。它除了继承 BeanFactory的所有功能外,还添加了对国际 化、资源访问、事件传播等方面的良好支持。
容器实现类
ApplicationContext有以下三个常用实现类:
- ClassPathXmlApplicationContext:该类可以从项目中读取配置文件
- FileSystemXmlApplicationContext:该类从磁盘中读取配置文件 (磁盘路径)
- AnnotationConfigApplicationContext:使用该类不读取配置文件,而是会读取注解
//创建spring容器
//ApplicationContext ac =new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac =new FileSystemXmlApplicationContext("E:\\Idea_Project\\springnb\\spring_ioc1\\src\\main\\resources\\bean.xml");
IOC_对象的创建方式
1.使用构造方法
Spring默认使用类的空参构造方法创建bean:假如类没有空参构造方法,将无法完成bean的创建。
2.使用工厂类的方法
Spring可以调用工厂类的方法创建bean(其实就是实例工厂):
- 创建工厂类,工厂类提供创建对象的方法:
public class StudentDaoFactory { public StudentDao getStudentDao(){ return new StudentDaoImpl(); } }
-
在配置文件中配置创建bean的方式为工厂方式。
<!-- id:工厂对象的id,class:工厂类 --> <bean id="studentDaoFactory" class="com.dream.dao.StudentDaoFactory"></bean> <!-- id:bean对象的id,factory-bean:工厂对象的id,factory-method:工厂方法 --> <bean id="studentDao" factory-bean="studentDaoFactory" factory-method="getStudentDao"></bean>
3.使用工厂类的静态方法
Spring可以调用工厂类的静态方法创建bean(就是静态工厂):
- Spring可以调用工厂类的静态方法创建bean:
public class StudentDaoFactory { public static StudentDao getStudentDao() { return new StudentDaoImpl(); } }
-
在配置文件中配置创建bean的方式为工厂静态方法。
<!-- id:bean的id class:工厂全类名 factory-method:工厂静态方法 --> <bean id="studentDao" class="com.dream.dao.StudentDaoFactory" factory-method="getStudentDao"></bean>
注意: spring 不能使用有参构造方法创建bean对象,(我感觉这个是可以依赖注入使用)
IOC_对象的创建策略
Spring通过配置 <bean>中的 scope 属性设置对象的创建策略,共有五 种创建策略:
- singleton:单例,默认策略。整个项目只会创建一个对象,通 过<bean>中的 lazy-init 属性可以设置单例对象的创建时机:
lazy-init="false"(默认):立即创建,在容器启动时会创建配 置文件中的所有Bean对象。饿汉式
lazy-init="true":延迟创建,第一次使用Bean对象时才会创建。饿汉式
<bean id="studentDao" class="com.dream.dao.StudentDaoImpl" scope="singleton" lazy-init="false"></bean>
- prototype:多例,每次从容器中获取时都会创建对象。
<!-- 配置多例策略 --> <bean id="studentDao" class="com.dream.dao.StudentDaoImpl" scope="prototype"></bean>
- request:每次请求创建一个对象,只在web环境有效。
- session:每次会话创建一个对象,只在web环境有效。
- gloabal-session:一次集群环境的会话创建一个对象,只在web 环境有效。
IOC_对象的销毁时机
对象的创建策略不同,销毁时机也不同:
- singleton:对象随着容器的销毁而销毁。 (整个项目就一个对象),正常情况是不会随意销毁的
- prototype:使用JAVA垃圾回收机制销毁对象。
- request:当处理请求结束,bean实例将被销毁。
- session:当HTTP Session最终被废弃的时候,bean也会被销毁掉。
- gloabal-session:集群环境下的session销毁,bean实例也将被销毁。
IOC_生命周期方法
Bean对象的生命周期包含:创建——使用——销毁,Spring可以配置 Bean对象在创建和销毁时自动执行的方法:
- 定义生命周期方法
public class StudentDaoImpl implements StudentDao{ // 创建时自动执行的方法 public void init(){ System.out.println("创建了!!!"); } // 销毁时自动执行的方法 public void destory(){ System.out.println("销毁了!!!"); } }
- 配置生命周期方法
<!-- init-method:创建对象时执行的方法 destroy-method:销毁对象时执行的方法 --> <bean id="studentDao" class="com.dream.dao.StudentDaoImpl"scope="singleton" init-method="init" destroy-method="destory"></bean>
-
测试
@Test public void t3(){ // 创建Spring容器 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); // 销毁Spring容器,ClassPathXmlApplicationContext有销毁容器的方法 ac.close(); }
IOC_获取Bean对象的方式
Spring有多种获取容器中对象的方式:
通过id/name获取
- 配置文件
<!--id 和name 都代表对象名字--> <bean name="studentDao" class="com.dream.dao.StudentDaoImpl2"></bean> <bean id="studentDao" class="com.dream.dao.StudentDaoImpl2"></bean>
-
获取对象
StudentDao studentDao = (StudentDao)ac.getBean("studentDao");
通过类型获取
- 配置文件
<bean name="studentDao" class="com.dream.dao.StudentDaoImpl2"></bean>
-
获取对象
StudentDao studentDao2 = ac.getBean(StudentDao.class);
可以看到使用类型获取不需要强转。
通过类型+id/name获取
虽然使用类型获取不需要强转,但如果在容器中有一个接口的多个实现类对象,则获取时会报错,此时需要使用类型+id/name获取
- 配置文件
<!--一个接口的两个实现类--> <bean name="studentDao" class="com.dream.dao.StudentDaoImpl2"></bean> <bean name="studentDao1" class="com.dream.dao.StudentDaoImpl"></bean>
-
获取对象
StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);