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

【Spring框架】

  • 一、引言
  • 二、Spring核心概念
  • 三、Spring入门示例
  • 四、进一步了解Spring的依赖注入
  • 五、Spring的面向切面编程(AOP)
  • 六、总结

一、引言

Spring框架自2003年发布以来,凭借其轻量级、易于扩展的特性,在Java企业级应用开发领域得到了广泛的应用。本文旨在帮助初学者快速掌握Spring框架的基本使用,并为进阶学习打下基础。

二、Spring核心概念

  1. 控制反转(IoC)
  2. 依赖注入(DI)
  3. 面向切面编程(AOP)

三、Spring入门示例

  1. 添加Spring依赖
    首先,在项目的pom.xml文件中添加Spring的依赖:
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.9</version></dependency>
</dependencies>
  1. 创建Bean
    创建一个名为User的Java类,用于表示用户信息:
public class User {private String name;private int age;// 构造函数、设值方法和toString方法省略
}
  1. 配置Spring容器
    在src/main/resources目录下创建一个名为applicationContext.xml的配置文件,配置User类的Bean:
<?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"><bean id="user" class="com.example.demo.User"><property name="name" value="张三"/><property name="age" value="25"/></bean>
</beans>
  1. 获取Bean
    在测试类中,通过Spring容器获取User对象:
public class DemoTest {@Testpublic void test() {// 加载配置文件,创建Spring容器ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取BeanUser user = (User) context.getBean("user");System.out.println(user);}
}

运行测试类,输出结果如下:

User{name='张三', age=25}

四、进一步了解Spring的依赖注入

  1. 构造器注入
  2. 设值注入
  3. 自动装配
    以下是设值注入的示例:
    首先,确保User类中有相应的setter方法:
public class User {private String name;private int age;// 省略构造函数和其他代码public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}
}

然后,在applicationContext.xml中配置设值注入:

<bean id="user" class="com.example.demo.User"><property name="name" value="王五"/><property name="age" value="28"/>
</bean>

五、Spring的面向切面编程(AOP)

以下是一个简单的AOP示例:
11. 添加AOP依赖
在pom.xml中添加Spring AOP的依赖:

<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.3.9</version>
</dependency>
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.7</version>
</dependency>
  1. 创建切面类
    创建一个名为LoggingAspect的类,用于记录方法调用的日志:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoggingAspect {@Pointcut("execution(* com.example.demo.UserService.*(..))")public void pointcut() {}@Before("pointcut()")public void beforeAdvice() {System.out.println("方法执行前,记录日志...");}
}
  1. 配置AOP
    在applicationContext.xml中配置AOP:
<aop:aspectj-autoproxy/>
<bean id="loggingAspect" class="com.example.demo.LoggingAspect"/>
  1. 创建目标类
    创建一个名为UserService的类,作为AOP的目标:
public class UserService {public void addUser() {
    System.out.println("添加用户...");
}

}

在applicationContext.xml中配置UserService的Bean:
```xml
<bean id="userService" class="com.example.demo.UserService"/>
  1. 测试AOP
    在测试类中,通过Spring容器获取UserService对象,并调用addUser方法:
public class AopTest {@Testpublic void testAop() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) context.getBean("userService");userService.addUser();}
}

运行测试类,输出结果如下:

方法执行前,记录日志...
添加用户...

从输出结果可以看出,在UserService的addUser方法执行前,LoggingAspect的beforeAdvice方法被调用,成功实现了AOP的日志记录功能。

六、总结

Spring框架的功能远不止于此,它还包括Spring MVC、Spring Data、Spring Security等多个模块,每个模块都有其独特的应用场景。

http://www.lryc.cn/news/427069.html

相关文章:

  • uniapp 日常业务 随便写写 源码
  • 【软件测试】单元测试20套练习题
  • 8.16 day bug
  • 《Nginx核心技术》第11章:实现MySQL数据库的负载均衡
  • 使用 Gnosis Safe 创建多签名钱包
  • LeetCode 算法:前 K 个高频元素 c++
  • MySQL的SQL语句更新某个字段的值在原来值的基础上随机增加100~600
  • LeetCode --- 410周赛
  • 最佳的iPhone解锁软件和应用程序
  • 初等函数和它的表达式
  • Android 12系统源码_多屏幕(二)模拟辅助设备功能开关实现原理
  • 【Go语言初探】(二)、项目文件结构和GOPATH设置
  • 三种简单排序:插入排序、冒泡排序与选择排序 【算法 05】
  • Python -- GUI图形界面编程—GUI编程实例 博主也在持续学习中[ 持续更新中!!! 欢迎白嫖 也求粉啊啊啊~ ]
  • Vue2和Vue3中的diff算法
  • springboot使用aop或Jackson进行数据脱敏
  • 【Solidity】基础介绍
  • 【SpringBoot3】双向实时通讯 websocket
  • 搭建内网开发环境(一)|基于docker快速部署开发环境
  • MATLAB R2023b配置Fortran编译器
  • 2024新型数字政府综合解决方案(七)
  • 搭建高可用k8s集群
  • 完美解决html2canvas + jsPDF导出pdf分页内容截断问题
  • 14 地址映射
  • Java Resilience4j-RateLimiter学习
  • Nginx--地址重写Rewrite
  • webflux源码解析(1)-主流程
  • ipad作为扩展屏的最简单方式
  • 【卡码网Python基础课 17.判断集合成员】
  • 生物研究新范式!AI语言模型在生物研究中的应用