Spring-01,Spring入门介绍,核心容器,工程搭建,控制反转与依赖注入,Bean的配置(未完)

Spring框架

1.什么是Spring?

Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能

1.1spirng的优点

1.方便解耦,简化开发

通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。有了Spring,用户不必再为单实例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。

2.AOP编程的支持

通过Spring提供的AOP功能,方便进行面向切面的编程,许多不容易用传统OOP实现的功能可以通过AOP轻松应付。

3.声明式事务的支持

在Spring中,我们可以从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活地进行事务的管理,提高开发效率和质量。

4.方便程序的测试

可以用非容器依赖的编程方式进行几乎所有的测试工作,在Spring里,测试不再是昂贵的操作,而是随手可做的事情。例如:Spring对Junit4支持,可以通过注解方便的测试Spring程序。

5.方便集成各种优秀框架

Spring不排斥各种优秀的开源框架,相反,Spring可以降低各种框架的使用难度,Spring提供了对各种优秀框架(如Struts,Hibernate、Hessian、Quartz)等的直接支持。

6.降低Java EE API的使用难度

Spring对很多难用的Java EE API(如JDBC,JavaMail,远程调用等)提供了一个薄薄的封装层,通过Spring的简易封装,这些Java EE API的使用难度大为降低。
在这里插入图片描述

2.Spring核心容器

Spring容器会负责控制程序之间的关系,而不是由程序代码直接控制。Spring为我们提供了两种核心容器,分别为BeanFactory和ApplicationContext

2.1 BeanFactory

创建BeanFactory实例时,需要提供Spring所管理容器的详细配置信息,这些信息通常采用XML文件形式来管理,其加载配置信息的语法如下:

 BeanFactory beanFactory = 
           new XmlBeanFactory(new FileSystemResource("F: /applicationContext.xml"));

2.2 ApplicationContext

继承了BeanFactory,不但实现了的功能,并且能够增加国际化,资源访问,事件传播等

常用实现类

ClassPathXmlApplicationContext 主要用户获取 resource 路径下的配置

	//大多数使用这种方式
ApplicationContext applicationContext = new 			                               						 ClassPathXmlApplicationContext("classpath:bean01.xml");
Student student = applicationContext.getBean(Student.class);

FileSystemXmlApplicationContext 主要获取绝对路径的配置

		ApplicationContext applicationContext = new 								   				           FileSystemXmlApplicationContext("src\\main\\resources\\bean01.xml");
Student student = applicationContext.getBean(Student.class);

3.工程搭建

3.1 创建java工程,引入依赖

<properties>
        <!--在当前pom 或者父类pom 中声明属性  -->
        <spirng.version>5.0.16.RELEASE</spirng.version>
</properties>

<!-- 引入spring 核心依赖  -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spirng.version}</version>
</dependency>

3.2 在resource创建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>就是一个对象
    -->
    <!--  一个bean 就是一个对象  就是让容器创建一个对象-->
    <bean id="student" class="com.yth.entity.Student">
        <!--  设置属性值-->
        <property name="id" value="100"></property>
        <property name="name" value="赵四"></property>
    </bean>
</beans>

3.测试

@Test
public void Test01(){
    
    
    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("src\\main\\resources\\bean01.xml"));
    Student student = beanFactory.getBean(Student.class);
    System.out.println(student);
}
@Test
public void Test02(){
    
    
    ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("classpath:bean01.xml");
    Student student = applicationContext.getBean(Student.class);
    System.out.println(student);
}
@Test
public void Test03(){
    
    
    ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src\\main\\resources\\bean01.xml");
    Student student = applicationContext.getBean(Student.class);
    System.out.println(student);
}

4.控制反转与依赖注入

4.1 控制反转IOC

控制反转,IOC。在使用Spring框架之后,**对象的实例不再由调用者来创建,而是由Spring容器来创建,**Spring容器会负责控制程序之间的关系,而不是由调用者的程序代码直接控制。这样,控制权由应用代码转移到了Spring容器,控制权发生了反转,这就是控制反转。

将**创建对象的权力交给容器,我们只需要告诉容器你需要常见那些对象就可以了,并且容器会一直持有这个对象,管理对象的声明周期,通过getBean()**获取对象

自建创建

// 自己手动创建对象
        Student student = new Student();

容器创建

  		// spring容器获取对象步骤:
        // 1.bean.xml 声明要创建的对象
        // 2.使用BeanFactory 的子类 去加载 bean.xml 配置 ,按照配置文件创建 bean对象,并且放置在容器中
        // 3. getBean(Student.class);  使用getBean 根据类型获取  

4.2 依赖注入DI

DI的全称是Dependency Injection,中文称之为依赖注入

从Spring容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,这相当于为调用者注入了它依赖的实例,这就是Spring的依赖注入。

依赖注入主要用来控制容器中对象之间的依赖关系

	<!-- 声明一个id为studentDao的实例   -->
    <bean id="studentDao" class="com.yth.dao.impl.StudentDaoImpl"></bean>
    <!-- 通过ref="iStudentDao"  容器中idiStudentDao 的对象 设置到StudentServiceImpl 对应studentDao 属性内  -->
    <bean id="studentService" class="com.yth.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"></property>
    </bean>

测试

 ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("classpath:bean01.xml");
        StudentService studentService = (StudentService) applicationContext.getBean("studentService");
        // 不需要手动控制依赖 使用容器控制依赖关系
        // studentService.setStudentDao(StudentDao);
        Student student = studentService.getStudentByID(10);
        System.out.println(student);

5. Bean的配置

5.1 什么是Bean?

如果把Spring看做一个大型工厂,则Spring容器中的Bean就是该工厂的产品。要想使用这个工厂生产和管理Bean,就需要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起。

Bean的本质就是Java中的对象,而Spring中的Bean其实就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean 。

5.2 Bean的属性

Bean的配置使用xml配置,XML配置文件的根元素是,中包含了多个子元素,每一个子元素定义了一个Bean,并描述了该Bean如何被装配到Spring容器中。关于元素的常用属性如下表所示:

image-20200520230325929

注意:

id必须是Bean中唯一的,而name可以有多个

<!--  一个bean 就是一个对象  就是让容器创建一个对象
    id 容器创建对象的唯一标识
    name 也为对象取一个标识 name  name可以有多个 使用,或者; 分割
-->
<bean id="student" name="zhaosi,si" class="com.yth.entity.Student">
    <!--  设置属性值-->
    <property name="id" value="100"></property>
    <property name="name" value="赵四"></property>
</bean>
//根据属性名获取student
@Test
public void Test05(){
    
    
    ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src\\main\\resources\\bean01.xml");
    Student student = (Student) applicationContext.getBean("zhaosi");
    System.out.println(student);
    Student student2 = (Student) applicationContext.getBean("si");
    System.out.println(student2);
}

5.3 Bean的实例化

在Spring中,要想使用容器中的Bean,也需要实例化Bean。实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化和实例工厂方式实例化(其中最常用的是构造器实例化)。

5.3.1 默认无参构造方法

容器调用无参构造方法创建对象

5.3.2 静态工厂实例方法

好处就是:可以自定创建的对象,并且将对象交给容器管理

创建静态工厂

package com.yth.Factory;
import com.yth.entity.Student;
/**
 * @author yth
 * @date 2020/10/19 - 20:33
 */
public class StaticStudentFactory {
    
    
    /**
     * 自定义 Student 对象的创建
     * 获取了创建对象的权力
     * @return
     */
    public static Student createStudent(){
    
    
        Student student = new Student();
        // 比如name 去网络查询 数据库查询
        student.setName("广坤");
        student.setId(101);
        return student;
    }
}

声明使用工厂创建的对象

	<!--
        bean 像容器声明创建对象  此时创建对象是由静态工厂创建  而不是容器直接创建
        class="com.qfedu.factory.StaticStudentFactory" 创建对象的静态工厂
        factory-method="createStudent"  制定创建对象的静态工厂方法
    -->
<bean id="student" class="com.yth.Factory.StaticStudentFactory" factory-method="createStudent"></bean>

测试获取对象

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:Factory\\StaticStudentFactory.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student);

5.4.3 实例工厂实例化

作用和静态工厂实例化一致

创建实例工厂

package com.yth.Factory;
import com.yth.entity.Student;
/**
 * @author yth
 * @date 2020/10/19 - 20:48
 */
public class StudentFactory {
    
    
    public Student creatStudent(){
    
    
        Student student = new Student();
        // 自定义初始化  student
        student.setName("xiaoming");
        student.setId(1000);
        return student;
    }
}

声明实例工厂并用实例工厂创建对象

<!--  声明实例工厂 创建实例工厂对象 -->
<bean id="studentFactory" class="com.yth.Factory.StudentFactory"></bean>
<!--
    通过实例工厂创建对象
    factory-bean="studentFactory"  引用实例工厂
    factory-method="creatStudent" 调用实例工厂的 creatStudent 创建自定义的Student对象 并放置到容器中
-->
<bean id="student" factory-bean="studentFactory" factory-method="creatStudent"></bean>

测试

//实例工厂创建bean
@Test
public void Test07(){
    
    
    ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:Factory\\StudentFactory.xml");
    Student student = (Student) applicationContext.getBean("student");
    System.out.println(student);
}

5.4.4 总结

静态工厂实例化和工厂实例化的作用是更具自己定义需求创建对象,主要用于dao层连接的初始(解决MySQL密码加密问题)

猜你喜欢

转载自blog.csdn.net/qq_41520945/article/details/109169029