【Spring技术】

ops/2024/9/23 9:50:28/

  • 1.Spring框架简介
    • 1.1 什么是框架?
    • 1.2 Spring框架诞生的技术背景
    • 1.3 Spring框架的核心思想
    • 1.4 Spring框架的核心体系
    • 1.5 Spring框架的特点
    • 1.6我对Spring的理解
  • 2.使用Spring完成第一个demo
    • 2.1 操作步骤
    • 2.2 Maven工程的创建
    • 2.3 Maven工程如何引入jar包
    • 2.4 使用Spring完成第一个demo
    • 2.4.1 创建Maven工程
    • 2.4.2 创建Spring配置文件
    • 2.4.3 创建服务类
    • 2.4.4 编写测试类
    • 2.4.5 运行测试类
  • 3.Spring常用注解
      • `@Component`
      • `@Service`
      • `@Repository`
      • `@Controller`
      • `@Autowired`
      • `@Qualifier`
      • `@Resource`
      • `@Value`
      • `@PostConstruct` 和 `@PreDestroy`
      • `@Bean`
      • `@Configuration`

1.Spring框架简介

1.1 什么是框架?

框架是一种具备通用性的结构体,能够加速产品功能实现的过程。例如,建筑框架、写作文章的结构框架等。

1.2 Spring框架诞生的技术背景

在多年前的Web开发中,开发者面临诸多问题,尤其是在基于三层模型开发时,业务逻辑代码中充斥着大量手动创建对象的代码,导致高度耦合。为了解决这些问题,Spring框架应运而生。

1.3 Spring框架的核心思想

Spring框架通过引入工厂模式解决了对象创建和依赖管理的问题,降低了代码之间的耦合度。它的核心思想包括控制反转(IoC)和依赖注入(DI),使得对象的创建和依赖关系由Spring框架来管理,而非在代码中硬编码。

1.4 Spring框架的核心体系

Spring框架的核心体系由以下几个模块组成:

  • spring-core:提供框架的基础功能,包括IoC和依赖注入。
  • spring-beans:提供BeanFactory,实现工厂模式,解耦配置和依赖。
  • spring-context:建立在core和beans模块之上,提供上下文管理,支持国际化、事件传播、资源加载等功能。
  • spring-context-support:提供对第三方集成的支持,如缓存、邮件、调度、模板引擎等。
  • spring-expression:提供强大的表达式语言,用于在运行时查询和操作对象图。

1.5 Spring框架的特点

  1. 方便解耦,简化开发:Spring作为一个大工厂,管理所有对象的创建和依赖关系。
  2. 方便集成各种优秀框架:Spring支持与多种优秀框架集成,如Struts2、Hibernate、MyBatis等。
  3. 降低Java EE API的使用难度:Spring对一些难用的Java EE API(如JDBC、JavaMail)提供了封装,简化了应用开发。
  4. 方便程序的测试:Spring支持JUnit4,可以通过注解方便地测试Spring程序。
  5. AOP编程的支持:Spring提供面向切面编程,简化权限拦截和运行监控等功能的实现。
  6. 声明式事务的支持:通过配置即可管理事务,无需手动编程。

1.6我对Spring的理解

  • 非侵入式:Spring开发的应用对象可以不依赖于Spring的API。
  • 控制反转(IoC):对象的创建和依赖关系的维护交给Spring管理。
  • 依赖注入(DI):依赖的对象通过配置自动注入,无需手动设置。
  • 面向切面编程(AOP):Spring提供AOP支持,简化权限拦截和监控等功能。
  • 容器:Spring是一个容器,管理应用对象的生命周期。
  • 组件化:Spring通过简单的组件配置组合复杂应用。
  • 一站式:Spring整合了多种企业应用开源框架和第三方类库。

2.使用Spring完成第一个demo

2.1 操作步骤

  1. 导入Spring框架的jar包。
  2. 创建用于配置bean对象的xml文件。
  3. 编写测试代码,从Spring容器中获取对象。

2.2 Maven工程的创建

  1. 创建Maven工程并配置pom.xml文件。
  2. 通过Maven仓库搜索并引入Spring-context依赖。

2.3 Maven工程如何引入jar包

  1. 打开浏览器,搜索“maven rep”并进入Maven仓库。
  2. 在搜索栏输入“spring-context”,选择对应的Spring版本。
  3. 复制对应版本的坐标到项目的pom.xml文件中。

通过以上步骤,可以成功创建一个Maven工程并引入Spring框架,为后续的Spring应用开发打下基础。

2.4 使用Spring完成第一个demo

为了演示如何使用Spring框架完成一个简单的demo,我们将通过以下步骤来实现一个简单的Spring应用。

2.4.1 创建Maven工程

首先,我们需要创建一个Maven工程,并在pom.xml文件中添加Spring相关的依赖。

<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>com.example</groupId><artifactId>spring-demo</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.18</version></dependency></dependencies>
</project>

2.4.2 创建Spring配置文件

src/main/resources目录下创建一个名为applicationContext.xml的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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloService" class="com.example.service.HelloService"><property name="message" value="Hello, Spring!"/></bean></beans>

2.4.3 创建服务类

src/main/java/com/example/service目录下创建一个服务类HelloService

java">package com.example.service;import org.springframework.stereotype.Service;@Service
public class HelloService {private String message;public void setMessage(String message) {this.message = message;}public void showMessage() {System.out.println(message);}
}

2.4.4 编写测试类

src/main/java/com/example目录下创建一个测试类DemoTest

java">package com.example;import com.example.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class DemoTest {public static void main(String[] args) {// 加载Spring配置文件并初始化Spring容器ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 从Spring容器中获取helloService的beanHelloService helloService = context.getBean("helloService", HelloService.class);// 调用helloService的方法helloService.showMessage();// 关闭Spring容器context.close();}
}

2.4.5 运行测试类

运行DemoTest类,如果一切配置正确,你将在控制台看到输出:Hello, Spring!

这个简单的demo展示了如何使用Spring框架来管理bean的创建和依赖注入。通过Spring配置文件,我们定义了helloService的bean,并在测试类中通过Spring容器获取该bean的实例,然后调用其方法。这个过程展示了Spring框架的IoC和DI特性。

3.Spring常用注解

Spring提供了一系列的注解来简化Java应用的开发。以下是一些常用的Spring注解及其代码演示。

@Component

用于标注一个类为Spring组件,通常用于非服务层的单例对象。

java">import org.springframework.stereotype.Component;@Component
public class MyComponent {public void doWork() {System.out.println("Doing some work...");}
}

@Service

用于标注服务层的组件。

java">import org.springframework.stereotype.Service;@Service
public class MyService {public void performService() {System.out.println("Performing service operations...");}
}

@Repository

用于标注数据访问层的组件,即DAO组件。

java">import org.springframework.stereotype.Repository;@Repository
public class MyRepository {public void accessData() {System.out.println("Accessing data...");}
}

@Controller

用于标注控制层的组件,即Spring MVC中的控制器。

java">import org.springframework.stereotype.Controller;@Controller
public class MyController {public String handleRequest() {System.out.println("Handling HTTP request...");return "responseView";}
}

@Autowired

用于自动注入依赖的组件。

java">import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Autowiredprivate MyService myService;public void doWork() {myService.performService();}
}

@Qualifier

当有多个相同类型的bean时,用于指定@Autowired要注入的确切的bean。

java">import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Autowired@Qualifier("specificService")private MyService myService;public void doWork() {myService.performService();}
}

@Resource

用于依赖注入,可以指定名称进行注入。

java">import javax.annotation.Resource;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Resource(name="myService")private MyService myService;public void doWork() {myService.performService();}
}

@Value

用于注入外部配置的值。

java">import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Value("${my.property}")private String myProperty;public void doWork() {System.out.println("Property value: " + myProperty);}
}

@PostConstruct@PreDestroy

用于标注在bean的生命周期中的初始化后和销毁前需要执行的方法。

java">import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@PostConstructpublic void init() {System.out.println("Bean is initialized.");}@PreDestroypublic void destroy() {System.out.println("Bean is destroyed.");}public void doWork() {System.out.println("Doing some work...");}
}

@Bean

用于在配置类中声明一个bean。

java">import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyService();}
}

@Configuration

用于标注一个类作为配置类,可以替代传统的XML配置文件。

java">import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {// 此类将扫描com.example包下的所有Spring组件
}

这些注解是Spring框架中常用的注解,通过它们可以大大简化Spring应用的开发工作。在实际的项目中,根据需要选择合适的注解来使用。


http://www.ppmy.cn/ops/99027.html

相关文章

jmeter中的json提取器

将响应结果提取作为变量 线程组->添加->后置处理器->json提取器 可以通过debug 调试器查看有没有提取出来 线程组->添加->取样器->debug sampler&#xff08;debug调试器&#xff09; 获取出来的响应数据。作为下一条接口的是否发送的判断内容 线程组…

搭建NFS服务器

搭建NFS服务器 记录linux下搭建使用NFS服务器的一般步骤&#xff0c;以ubuntu20.04和centos7.9操作进行记录。 1. 安装 NFS 服务器 运行以下命令安装 NFS 服务器。 # ubuntu下安装 sudo apt-get update sudo apt install nfs-kernel-server # 配置服务 sudo systemctl start …

鲲鹏服务器安装Kafka

由于项目需求&#xff0c;需要在鲲鹏云主机上安装Kafka&#xff0c;并且要求安装的版本为2.3.X。下面主要从以下几个步骤说明如何安装&#xff1a; 1、下载kafka的安装文件 2、上传到服务器 3、修改配置 4、启动 5、使用工具测试 服务器信息 CPU信息 [rootecs02 ~]# lscpu A…

besier打断和升阶,高阶性质

欢迎关注更多精彩 关注我&#xff0c;学习常用算法与数据结构&#xff0c;一题多解&#xff0c;降维打击。 问题描述 对besier曲线在u处打断&#xff0c;生成两条besier曲线对besier曲线升阶处理 bezier高阶性质 求导推导 P ( t ) ∑ i 0 n B i n ( t ) b i \boldsymbol …

封装el-table核心代码

问题背景 封装el-tabletable的时候有时候忘记语法了&#xff0c;然后问ai或者百度又出来一堆不太正确的回答&#xff0c;作此记录。 1.封装好的table组件完整代码&#xff08;基础版&#xff09; <template><div><el-table:data"tableData":height…

iis部署服务时,发现只能进行get请求,无法发起post、put请求

问题描述&#xff1a; iis部署服务时&#xff0c;发现只能进行get请求&#xff0c;无法发起post、put请求 问题原因&#xff1a; iis部署时&#xff0c;webDAV模块限制 解决方法&#xff1a; 1.搜索【服务器管理器】 2.点击【删除角色功能】 3.选中WebDAV&#xff0c;点…

OpenCV与AI深度学习 | 基于改进YOLOv8的景区行人检测算法

本文来源公众号“OpenCV与AI深度学习”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;基于改进YOLOv8的景区行人检测算法 作者&#xff1a;贵向泉&#xff0c;刘世清&#xff0c;李立等 来源&#xff1a;《计算机工程》期刊 编…

华为数通方向HCIP-DataCom H12-821题库(更新单选真题:21-30)

第21题 以下关于0SPF中ABR的描述,错误的是哪一项? A、ABR将连接的非骨干区域内的1类和2类1SA转换成3类LSA,发布到骨干区域中 B、ABR不能够产生4类和5类LSA C、ABR上有多个LSDB,ABR为每一个区域维护一个LSDB D、ABR将骨干区域内的1类、2类LSA和3类LSA转换成三类LSA,发布到…