学习笔记——Spring简介;Spring搭建步骤;Spring的特性;Spring中getBean三种方式;Spring中的标签

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

2023-01-13

一、Spring

1、Spring简介

(1)Spring是一个为简化企业级开发而生的开源框架。

(2)Spring是一个IOC(DI)和AOP容器框架。

IOC:Inversion of Contriol(控制反转,即将对象的控制权交给Spring)

AOP:Aspect-Oriented Programming,面向切面编程

DI:Dependency Injection(依赖注入)

(3)官方文档:

http://www.spring.io

2、搭建Spring框架步骤

(1)导入jar包

jar包可以在maven中查找

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

(2)编写核心配置文件

将配置文件命名为“applicationContext.xml(或者beans.xml或者spring.xml”

配置文件的路径在“src/main/resources”

(3)使用核心类库

3、创建Spring框架的步骤

(1)创建一个maven工程,命名为“day05_spring”

在“day05_spring/pom.xml”中添加jar包

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
</dependencies>

(2)在“day05_spring/src/main/java/com.hh.spring.pojo”下创建“Student”类

public class Student {
    private Integer stuId;
    private String stuName;

    public Student() {
    }

    public Student(Integer stuId, String stuName) {
        this.stuId = stuId;
        this.stuName = stuName;
    }

    public Integer getStuId() {
        return stuId;
    }

    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                '}';
    }
}

(3)在“day05_spring/src/main/resources”中创建“applicationContext.xml”

<?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="stuZhangsan" class="com.hh.spring.pojo.Student">
                <property name="stuId" value="101"></property>
                <property name="stuName" value="zhangsan"></property>
        </bean>
</beans>

(4)在“day05_spring/src/test/java”中设置测试类

public class TestSpring {
    @Test
    public void testSpring(){
        //创建容器对象
        ApplicationContext iocObj = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器对象,获取需要对象
        Student stuZhangsan = (Student)iocObj.getBean("stuZhangsan");
        System.out.println("stuZhangsan = " + stuZhangsan);
    }
}

4、Spring的特性

(1)非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API。

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

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

(4)一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了表述层的SpringMVC和持久层的JDBCTemplate)。

5、Spring中getBean三种方式

(1)方式一

getBean(String beanId):通过beanId获取对象

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

(2)方式二

getBean(Class clazz):通过Class方式获取对象

不足:容器中有多个相同类型bean的时候,会报错“expected single matching bean but found 2”

(3)方式三

getBean(String beanId,Clazz clazz):通过beanId和Class获取对象

常用

测试类的关键代码

Student stuZhangsan = iocObj.getBean("stuZhangsan", Student.class);
System.out.println("stuZhangsan = " + stuZhangsan);

6、Spring中的标签

(1)属性

①id:bean的唯一标识

②class:定义bean的类型(指定class全类名)

(2)子标签

①property:为对象中属性赋值(set注入)

name属性:设置属性名称

value属性:设置属性数值

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring