Spring 框架简介及 IDEA 配置
源码地址:https://github.com/busyforest/SpringLearning
一、Spring 框架简介
什么是 Spring 框架?
Spring 是一个于 2003 年兴起的一个轻量级的 Java 开发框架。在企业应用开发过程中,业务逻辑和前端代码等其他各层通常需要解耦合或者松耦合,以达到便于维护,便于后续添加扩展的目的。考虑到企业应用开发的复杂程度,这通常并不是一件简单的事。通过 Spring 等分层架构的框架以及 Spring MVC 等模式,开发者可以选择使用哪一种组件,再由框架集成到一起。
为什么学习 Spring 框架?
Spring 官网说:“We use a lot of the tools that come with the Spring framework and reap the benefits of having a lot of the out of the box solutions, and not having to worry about writing a ton of additional code—so that really saves us some time and energy.”
-
使用 Spring 框架可以让 Java 开发更加便捷,简单和安全。通过框架化可以省去大量额外的代码,让业务的核心逻辑更加突出。
-
在开发过程中,开发者可以自主选择组件,例如:
- MVC:Model,View and Controller. MVC 模式是软件工程中常见的一种软件架构模式,将项目划分为模型(Model)、视图(View)和控制器(Controller)三个部分,并赋予各个部分不同的功能,方便开发人员进行分组。在Spring中可以选择Struts2、SpringMVC等组件。
- ORM:Object Relational Mapping. 对象关系映射,用于实现面向对象编程语言里不同类型系统的数据之间的转换。在Spring中可以选择Hibernate、MyBatis、Spring Data等组件。
Spring 核心机制简介
- IoC 机制
Inversion of Control,即控制反转。在一般的 Java 程序中,创建对象通常由程序员通过 new 来完成。这种方法在对象之间的依赖关系不明显时可能没什么问题,但是在它们关系很复杂的时候靠程序员去维护会很绕,同时也会造成各个类之间严重的相互依赖和耦合。而在 IoC 机制中,创建和管理对象的任务交给了容器,我们只需要提前把对象的要求配置到容器中,用的时候直接从容器拿即可。拿做饭打个比方,做饭需要食材,一般情况下我们的食材可能需要我们自己去菜市场挑选和购买,而在使用 IoC 容器的时候我们只需要写一张单子,告诉 IoC 容器我们需要什么食材,每种食材的成色要怎么样,IoC 容器会帮我们去买菜。这样我们就可以把关注点放到具体怎么去处理这些食材上,也就是核心业务上。
实现 IoC 的方法有很多种,比较常用的是依赖注入(Dependency Injection)。我们只需要将被管理组件的 Constructor 或者 Setter 或者 Interface 暴露给 IoC 容器,容器就可以动态地将某种依赖关系的目标对象实例注入到应用系统中的各个关联的组件之中,从而实现对各个组件的管理。
- AOP 思想
Aspect Origented Programming,即面向切面编程。AOP 可以看做 OOP 的一种拓展。切面是一个模块化的关注点,例如日志消息,权限检查等等。例如,我们可能需要在用户每次下达命令的时候都检查一次用户的权限(当然我们也可以对用户划分信任区、未识别区和危险区来避免过多重复的权限检查),每次都调用权限检查方法固然是一种办法,但我们也可以通过 AOP 的方法,在不修改原始业务代码(不添加调用类似权限检查方法的语句)的情况下,实现对切面的统一处理。
二、IDEA 配置 Spring
这里通过 maven 来配置相关依赖项。
首先创建一个普通的新建项目,起名叫 Spring1,在构建系统中选择 Maven。
点击创建即可,在创建出的项目文件中会含有一个 pom.xml 文件。我们需要在这个文件中添加下列代码:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.4</version>
</dependency>
</dependencies>
添加完之后记得点击“加载 Maven 项目更改”,这样就导入了基本的 Spring 框架。
完整的 pom.xml
文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.4</version>
</dependency>
</dependencies>
</project>
下面来添加 Spring IoC 的配置文件,在 src/main/resources
中添加一个新的文件 spring.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">
</beans>
为了更好地与一般方法对比,我们可以创建一个 User
类(我这里是创建了一个软件包 com.busyforest.entity
,在软件包内添加了 User
类),代码如下:
package com.busyforest.entity;
public class User {
private int age;
private int uid;
private String name;
public void setAge(int age) {
this.age = age;
}
public void setUid(int uid) {
this.uid = uid;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return this.getClass()+"["+this.name+", "+this.uid
+", "+this.age+"]";
}
}
如上文所说,这里要把 Setter 方法暴露给 IoC 容器,同时重写了 toString()
方便查看信息。
回到 spring.xml
中,我们怎么完成对对象的创建、赋值和管理呢?通过配置标签实现。
在 beans
标签内添加代码:
<bean name="user1" class="com.busyforest.entity.User">
<property name="age" value="18"></property>
<property name="uid" value="12345"></property>
<property name="name" value="busyforest"></property>
</bean>
-
通过配置
bean
标签来完成对对象的管理。name
:对象名class
:对象模板类名id
:对象名(以前使用的,现在不常用,更多用name
)
-
通过配置
property
标签来完成对对象的赋值。-
name
:对象中成员变量的名字 -
value
:对象中成员变量的值(String、int等基本数据类型可以直接赋值,其他类型要用到ref
标签) -
ref
:将IoC中另外一个bean赋值给当前的变量(也就是DI的过程)
-
在 main/java
中添加一个 Test.java
,检查一下我们是否成功把 user1
添加到了容器中:
import com.busyforest.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = (User) applicationContext.getBean("user1");
System.out.println(user.toString());
}
}
运行 Test.java
,控制台输出:
class com.busyforest.entity.User[busyforest, 12345, 18]
进程已结束,退出代码为 0
说明基本的 Spring 已经配置成功了。