当前位置: 首页 > news >正文

【Spring技术】

  • 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

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

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组件,通常用于非服务层的单例对象。

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

@Service

用于标注服务层的组件。

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

@Repository

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

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

@Controller

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

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

@Autowired

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

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。

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

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

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

用于注入外部配置的值。

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的生命周期中的初始化后和销毁前需要执行的方法。

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。

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

@Configuration

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

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应用的开发工作。在实际的项目中,根据需要选择合适的注解来使用。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 校友会系统小程序的设计
  • 从web.xml动态读取sunspringmvc.xml文件
  • 重复的DNA序列(LeetCdoe)
  • 【Android】adb devices 出现devices offline的问题
  • HttpMessageNotReadableException
  • 开源微服务开发框架Pig
  • 「阅读」数据密集型系统设计 第六章 分区
  • 基于STM32开发的智能水箱液位控制系统
  • Tina-SDK开发
  • NVIDIA超分辨率开启详细教程
  • 【Redis】哈希类型详解及缓存方式对比:从命令操作到实际应用场景
  • 欧拉函数.
  • SCI论文系统各阶段状态含义,一文带你全面掌握!告别投稿小白!
  • Pandas_merge_join
  • 【IoT】将各类遥控器(红外,频射,蓝牙,wifi,Zigbee)等设备接入米家,实现家庭物联网设备控制(以极米Z7X投影仪为例)
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • axios 和 cookie 的那些事
  • ES学习笔记(12)--Symbol
  • Facebook AccountKit 接入的坑点
  • JavaScript类型识别
  • JS基础之数据类型、对象、原型、原型链、继承
  • Python学习笔记 字符串拼接
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • Sass Day-01
  • Sublime text 3 3103 注册码
  • zookeeper系列(七)实战分布式命名服务
  • 机器学习 vs. 深度学习
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 区块链分支循环
  • 设计模式(12)迭代器模式(讲解+应用)
  • 设计模式走一遍---观察者模式
  • 实习面试笔记
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 首页查询功能的一次实现过程
  • 智能合约Solidity教程-事件和日志(一)
  • PostgreSQL之连接数修改
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • # Spring Cloud Alibaba Nacos_配置中心与服务发现(四)
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (07)Hive——窗口函数详解
  • (23)Linux的软硬连接
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (STM32笔记)九、RCC时钟树与时钟 第一部分
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (三)Honghu Cloud云架构一定时调度平台
  • (转)LINQ之路
  • (转)Sql Server 保留几位小数的两种做法
  • *Django中的Ajax 纯js的书写样式1
  • .Net IE10 _doPostBack 未定义