首页 > 其他分享 >Spring入门

Spring入门

时间:2022-11-01 19:47:12浏览次数:53  
标签:容器 入门 Spring public IOC id name

一、Spring概述介绍

Spring 框架是一个开源的 Java 平台,它最初是由 Rod Johnson 编写的,并且于 2003 年 6 月首
次在 Apache 2.0 许可下发布。
Spring 是轻量级的框架,其基础版本只有 2 MB 左右的大小。
Spring 框架的核心特性是可以用于开发任何 Java 应用程序,但是在 Java EE 平台上构建 web 应
用程序是需要扩展的。 Spring 框架的目标是使 J2EE 开发变得更容易使用,通过启用基于 POJO
编程模型来促进良好的编程实践。

1、Spring Farmework

是 Spring 基础框架,可以视为 Spring 基础设施,基本上任何其他 Spring 项目都是以 Spring Framework
为基础的。

特性
  • 非侵入式:使用 Spring Framework 开发应用程序时,Spring 对应用程序本身的结构影响非常小。对领域模型可以做到零污染;对功能性组件也只需要使用几个简单的注解进行标记,完全不会破坏原有结构,反而能将组件结构进一步简化。这就使得基于 Spring Framework 开发应用程序时结构清晰、简洁优雅。
  • 控制反转:IOC——Inversion of Control,翻转资源获取方向。把自己创建资源、向环境索取资源变成环境将资源准备好,我们享受资源注入。
  • 面向切面编程:AOP——Aspect Oriented Programming,在不修改源代码的基础上增强代码功能。
  • 容器:Spring IOC 是一个容器,因为它包含并且管理组件对象的生命周期。组件享受到了容器化的管理,替程序员屏蔽了组件创建过程中的大量细节,极大的降低了使用门槛,大幅度提高了开发
    效率。
  • 组件化:Spring 实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用 XML和 Java 注解组合这些对象。这使得我们可以基于一个个功能明确、边界清晰的组件有条不紊的搭建超大型复杂应用系统。
  • 声明式:很多以前需要编写代码才能实现的功能,现在只需要声明需求即可由框架代为实现。
  • 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库。而且Spring 旗下的项目已经覆盖了广泛领域,很多方面的功能性需求可以在 Spring Framework 的基础上全部使用 Spring 来实现。
Spring Framework五大功能模块
功能模块 功能介绍
Core Container 核心容器,在 Spring 环境下使用任何功能都必须基于 IOC 容器。
AOP&Aspects 面向切面编程
Testing 提供了对 junit 或 TestNG 测试框架的整合。
Data Access/Integration 提供了对数据访问/集成的功能。
Spring MVC 提供了面向Web应用程序的集成功能。

二、IOC容器

IOC:Inversion of Control,翻译过来是反转控制。

在开发中时,我们不需要知道知道创建资源对象的方式,只需要提供接收对象的方式即可。将对象的创建、销毁交给spring进行管理。

1、DI

DI:Dependency Injection,翻译过来是依赖注入。

DI 是 IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如:setter 方法)接受来自于容器的资源注入。相对于IOC而言,这种表述更直接。
IOC 就是一种反转控制的思想, 而 DI 是对 IOC 的一种具体实现。

2、 IOC容器在Spring中的实现

IOC 容器中管理的组件也叫做 bean。在创建bean 之前,首先需要创建 IOC 容器。
Spring 提供了 IOC 容器的两种实现方式:

  • 1)BeanFactory
    这是 IOC 容器的基本实现,是 Spring 内部使用的接口。面向 Spring 本身,不提供给开发人员使用。
  • 2)ApplicationContext
    BeanFactory 的子接口,提供了更多高级特性。面向 Spring 的使用者,几乎所有场合都使用
    ApplicationContext 而不是底层的 BeanFactory。
  • ApplicationContext的主要实现类
    image
类型名 简介
ClassPathXmlApplicationContext 通过读取类路径下的 XML 格式的配置文件创建 IOC 容器 对象。
FileSystemXmlApplicationContext 通过文件系统路径读取 XML 格式的配置文件创建 IOC 容 器对象
ConfigurableApplicationContext ApplicationContext 的子接口,包含一些扩展方法 refresh() 和 close() ,让 ApplicationContext 具有启动、 关闭和刷新上下文的能力。
WebApplicationContext 专门为 Web 应用准备,基于 Web 环境创建 IOC 容器对 象,并将对象引入存入 ServletContext 域中。

三、XML方式管理bean

1、入门案例

  • 引入依赖
 <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包    -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.3.1</version>
</dependency>
  • 创建类
public class HelloWorld {
    public void sayHello(){
        System.out.println("Hello 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">

    <!--
        配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理
        通过bean标签配置IOC容器所管理的bean
        属性:
        id:设置bean的唯一标识
        class:设置bean所对应类型的全类名
    -->
    <bean id="helloworld" class="com.snow.pojo.HelloWorld"></bean>
</beans>
  • 测试
    @Test
    public void testSayHello(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloworld = ac.getBean("helloworld", HelloWorld.class);
        helloworld.sayHello();
    }

2、获取bean的三种方式

  • 根据id获取
HelloWorld bean = ac.getBean("helloworld");
  • 根据类型获取
HelloWorld bean = ac.getBean(HelloWorld.class);
  • 根据id和类型获取
HelloWorld bean = ac.getBean("helloworld", HelloWorld.class);

3、依赖注入的两种方式

实体类

public class Student {
	private Integer id;
	private String name;
	private Integer age;
	private String sex;

	public Student() {

	}

	public Student(Integer id, String name, Integer age, String sex) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}


	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
}
  • setter方式注入
    <bean id="studentTwo" class="com.atguigu.spring.pojo.Student">
        <!--
            property:通过成员变量的set方法进行赋值
            name:设置需要赋值的属性名(和set方法有关)
            value:设置为属性所赋的值
        -->
        <property name="sid" value="1001"></property>
        <property name="sname" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="gender" value="男"></property>
    </bean>
  • 构造器注入
    constructor-arg标签还有两个属性可以进一步描述构造器参数:
    index:指定参数所在位置的索引(从0开始)
    name:指定参数名
    <bean id="studentThree" class="com.atguigu.spring.pojo.Student">
        <constructor-arg value="1002"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
        <constructor-arg value="24" name="age"></constructor-arg>
    </bean>

4、为类、数组、集合类型属性赋值

  • 类属性赋值
<!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值    --> 
<property name="clazz" ref="clazzOne"></property>
  • list集合属性赋值
<property name="students">
	<list>
		<ref bean="studentOne"></ref>
		<ref bean="studentTwo"></ref>
		<ref bean="studentThree"></ref>
	</list>
</property>
  • map集合属性赋值
<property name="teacherMap">
	<map>
		<entry key="10086" value-ref="teacherOne"></entry>
		<entry key="10010" value-ref="teacherTwo"></entry>
	</map>
</property>
  • 数组属性赋值
<property name="hobby">
	<array>
		<value>抽烟</value>
		<value>喝酒</value>
		<value>烫头</value>
	</array>
</property>

5、引入外部属性文件

  • 加入依赖
<!-- MySQL驱动 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.16</version>
</dependency>
<!-- 数据源 -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.31</version>
</dependency>
  • 创建外部文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
  • 引入外部文件
    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置数据源 -->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
  • 测试
    @Test
    public void dataSourceTest() throws SQLException {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource dataSource = ac.getBean(DruidDataSource.class);
        System.out.println(dataSource.getConnection());
    }

标签:容器,入门,Spring,public,IOC,id,name
From: https://www.cnblogs.com/Snowclod/p/16848858.html

相关文章