初识Spring:如何在Maven工程上搭建Spring框架?

编译软件:IntelliJ IDEA 2019.2.4 x64
操作系统:win10 x64 位 家庭版
Maven版本:apache-maven-3.6.3
Mybatis版本:3.5.6
spring版本:5.3.1



Spring系列专栏文章目录


一. 什么是Spring?

Spring 是一个开源的 Java 应用程序框架,由 Rod Johnson 在 2003 年创建。它为企业级应用程序的开发提供了全面的基础设施支持和丰富的功能特性,包括依赖注入、面向切面编程、事务管理、数据访问、Web 应用、消息传递等。

在这里插入图片描述

Spring 框架具有轻量级、可扩展性强、容易使用和优秀文档等诸多优点,广泛应用于各种类型的 Java 项目中。它是目前最流行的 Java 开发框架之一,已经成为 Java 社区的标准技术之一。

简而言之,Spring是一个IOC(DI)和AOP容器框架,为简化企业级开发而生的开源框架。

什么是IOC?

IOC,英文全称为Inversion of Control【控制反转】,即将对象【万物皆对象】控制权交整个Spring

什么是DI?

DI,英文全称为Dependency Injection,即依赖注入

什么是AOP?

AOP,英文全称为Aspect Oriented Programming ,即面向切面编程

spring官网地址:https://spring.io/

使用spring获取对象前后对比:

在这里插入图片描述


二. 如何搭建Spring框架?(入门案例)

步骤:

  1. 导入spring的相关jar包
  2. 编写配置文件(将对象装配到IOC中)
  3. 使用核心类库

入门案例:创建Student类,在配置文件中将该类对象stuTest装配到IOC中,设置相应属性,最后使用Spring框架其核心类库获取并打印对象stuTest的信息。

准备:建立Student类

在这里插入图片描述

①导入spring的相关jar包

代码示例如下:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.1</version>
</dependency>

②编写配置文件(将对象装配到IOC容器中)

配置文件命名一般命名为applicationContext.xml【beans.xml或spring.xml】

位置src/main/resources目录下

代码示例如下:

<?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">

    <!-- 将对象装配到IOC容器中-->
     <bean id="stuTest" class="spring.pojo.Student">
         <!--  给对象stuTest添加属性          -->
         <property name="id" value="101"></property>
         <property name="name" value="jack"></property>
     </bean>
</beans>

③使用核心类库

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.pojo.Student;

public class TestSpring {
    
    

    @Test
    public void test01(){
    
    
         //创建容器对象(spring是一个容器)
        ApplicationContext iocObj=new 					    		                             ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器对象获取需要的对象
        Student stuTest = (Student) iocObj.getBean("stuTest");
        System.out.println(stuTest);

    }

}

在这里插入图片描述


三 Spring有哪些特性?

  • 非侵入式基于Spring开发的应用中的对象可以不依赖于Spring的API

    如何理解?

    对比Servlet,Servlet是侵入式的,当我们想要使用Servlet时以创建一个Servlet类,它继承HttpServlet,看似没有实现Servlet接口,但它所继承的父类HttpServle自己实现了Servlet接口。故此像这种实现功能必须继承或实现某个接口,被称为侵入式;使用Spring开发就不一样,它是直接将对象装配到IOC中,测试运行时只需要创建容器对象,再从中获取需要的对象。

  • 容器Spring是一个容器,因为它包含并且管理应用对象的生命周期。

    如何理解?

    在应用开发时,我们只需要将对象装入spring这个容器中,并且需要时直接从里拿

  • 组件化Spring实现了使用简单的组件配置组合成一个复杂的应用。在Spring中可以使用XML和java注解组合使用这些对象

  • 一站式在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库

    ps:实际上Spring自身也提供了表述层的SpringMVC和持久层的DBCTemplate


四. Spring中如何获取容器中的对象?(getBean()方法)

getBean()方法有以下五种重载形式,如下图所示。

在这里插入图片描述

ps:这里暂时只讨论前三种

方式:

  • getBean(String beanId)通过beanld获取对象

    不足:需要强制类型转换,不灵活

    示例代码如下:

    public void test01(){
          
          
        //创建容器对象(spring是一个容器)
        ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器对象获取需要的对象
        Student stuTest = (Student) iocObj.getBean("stuTest");
        System.out.println(stuTest);
    
    }
    
  • getBean(Class aClass)通过Class方式获取对象

    不足:容器中有多个相同类型bean的时候,会报如下错误:

    expected single matching bean but found 2:stuZhenzhong,stuZhouxu
    

    案例演示:在配置文件中装配两个相同类型的对象进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">
    
        <!-- 将对象装配到IOC容器中-->
         <bean id="stuTest" class="spring.pojo.Student">
             <!--  给对象stuTest添加属性          -->
             <property name="id" value="101"></property>
             <property name="name" value="jack"></property>
         </bean>
    
        <bean id="stulisi" class="spring.pojo.Student">
            <!--  给对象stuTest添加属性          -->
            <property name="id" value="102"></property>
            <property name="name" value="李四"></property>
        </bean>
    
    
    </beans>
    

    ②测试运行

    @Test
    public void test01(){
          
          
        //创建容器对象(spring是一个容器)
        ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器对象获取需要的对象
    
       /*
       //方式1
        Student stuTest = (Student) iocObj.getBean("stuTest");
        System.out.println(stuTest);
        */
    
       //方式2
        Student bean = iocObj.getBean(Student.class);
        System.out.println(bean);
    }
    

    在这里插入图片描述
    在这里插入图片描述

  • getBean(String beanld,Class aClass)通过beanld和Class获取对象

    优势:使用此方式可避免需要强制类型转和规避因为容器内存在多个相同类型的对象而产生的报错问题

    非常推荐使用!!!

    示例代码如下:

    @Test
    public void test01(){
          
          
        //创建容器对象(spring是一个容器)
        ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器对象获取需要的对象
    
       /*
       //方式1
        Student stuTest = (Student) iocObj.getBean("stuTest");
        System.out.println(stuTest);
        */
    
       //方式2
    /*    Student bean = iocObj.getBean(Student.class);
        System.out.println(bean);*/
    
        //方式3
        Student stuTest = iocObj.getBean("stuTest", Student.class);
        System.out.println(stuTest);
    
    }
    

    在这里插入图片描述

注意:

框架默认都是通过无参构造器帮助我们创建对象。所以如提供对象的构造器时,一定要添加无参构造器


五. 详解配置文件中的bean标签

属性:

  • idbean的唯一标识
  • class定义bean的类型【class全类名】

子标签:

  • property属性为对象中属性赋值【set注入】
  • name属性设置属性名称
  • value属性设置属性数值

示例代码如下:

  <!-- 将对象装配到IOC容器中-->
     <bean id="stuTest" class="spring.pojo.Student">
         <!--  给对象stuTest添加属性          -->
         <property name="id" value="101"></property>
         <property name="name" value="jack"></property>
    </bean>

详解如下:

该 XML 配置定义了一个名为 “stuTest” 的 Spring Bean,并将其实现类设置为 “spring.pojo.Student”。Bean 具有两个属性:“id” 和 “name”,分别被设置为整数值 101 和字符串值 “jack”。

这里使用的是 set 方法注入属性值,即通过调用 setter 方法将某些值传递给对象。具体来说,这里使用了 property 元素来配置属性注入,其中 name 属性指定 Bean 类中相应属性的名称,value 属性则指定要注入的属性值。

猜你喜欢

转载自blog.csdn.net/siaok/article/details/130523684
今日推荐