万网定制网站怎么样,重生做网站的小说,layui做的网站,新手学做网站要花钱么Spring Framework入门指南#xff1a;从零开始构建企业级应用 #x1f680; #x1f3af; 掌握Spring框架#xff0c;助你成为Java企业级开发高手#xff01; 在上一篇文章中#xff0c;我们学习了Java入门系列的最后一篇文章–Java反射机制。今天开始#xff0c;让我们…Spring Framework入门指南从零开始构建企业级应用 掌握Spring框架助你成为Java企业级开发高手 在上一篇文章中我们学习了Java入门系列的最后一篇文章–Java反射机制。今天开始让我们一起深入探索Spring框架这个Java领域最受欢迎的企业级开发框架
考虑到相关内容较多咱们这个系列分为四个部分进行更新
废话不多说开始第一部分
1. Spring框架简介
1.1 什么是Spring Framework
Spring Framework是一个轻量级的开源应用框架它为开发Java应用提供了全面的基础设施支持。Spring的核心特性是依赖注入DI和面向切面编程AOP。
1.2 为什么选择Spring 模块化设计按需使用 松耦合易于测试 强大的生态系统 活跃的社区支持 广泛的企业应用
1.3 Spring的核心模块
spring-core
├── spring-beans # Bean容器和依赖注入
├── spring-context # 应用上下文邮件定时任务等
├── spring-aop # 面向切面编程
├── spring-jdbc # 数据库访问
├── spring-orm # 对象关系映射
├── spring-web # Web开发基础
└── spring-webmvc # Web MVC框架2. 快速开始
2.1 环境准备
首先在Maven项目中添加Spring依赖
dependencies!-- Spring核心依赖 --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.23/version/dependency!-- Spring Web MVC --dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.3.23/version/dependency!-- 数据库相关 --dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.23/version/dependency!-- Lombok可选 --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/versionscopeprovided/scope/dependency
/dependencies2.2 第一个Spring应用
创建一个简单的Spring应用展示依赖注入的基本用法
// 用户实体
Data
AllArgsConstructor
public class User {private Long id;private String username;private String email;
}// 用户服务接口
public interface UserService {User getUserById(Long id);void createUser(User user);
}// 用户服务实现
Service
Slf4j
public class UserServiceImpl implements UserService {Autowiredprivate UserRepository userRepository;Overridepublic User getUserById(Long id) {log.info(获取用户信息: {}, id);return userRepository.findById(id);}Overridepublic void createUser(User user) {log.info(创建用户: {}, user);userRepository.save(user);}
}// 应用配置类
Configuration
ComponentScan(com.example.demo)
public class AppConfig {Beanpublic UserRepository userRepository() {return new UserRepositoryImpl();}
}// 主类
public class MainApplication {public static void main(String[] args) {ApplicationContext context new AnnotationConfigApplicationContext(AppConfig.class);UserService userService context.getBean(UserService.class);User user new User(1L, 张三, zhangsanexample.com);userService.createUser(user);User foundUser userService.getUserById(1L);System.out.println(找到用户 foundUser);}
}3. Spring核心特性详解
3.1 依赖注入DI
Spring提供了多种依赖注入方式
// 1. 构造器注入推荐
Service
public class OrderService {private final UserService userService;private final PaymentService paymentService;Autowiredpublic OrderService(UserService userService, PaymentService paymentService) {this.userService userService;this.paymentService paymentService;}
}// 2. Setter注入
Service
public class ProductService {private InventoryService inventoryService;Autowiredpublic void setInventoryService(InventoryService inventoryService) {this.inventoryService inventoryService;}
}// 3. 字段注入不推荐
Service
public class CartService {Autowiredprivate ProductService productService;
}3.2 面向切面编程AOP
AOP允许我们将横切关注点如日志、事务从业务逻辑中分离出来
// 日志切面
Aspect
Component
Slf4j
public class LoggingAspect {Around(annotation(LogExecutionTime))public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {long startTime System.currentTimeMillis();Object result joinPoint.proceed();long endTime System.currentTimeMillis();log.info(方法 {} 执行耗时: {}ms,joinPoint.getSignature().getName(),(endTime - startTime));return result;}AfterThrowing(pointcut execution(* com.example.demo.service.*.*(..)),throwing ex)public void logError(JoinPoint joinPoint, Exception ex) {log.error(方法 {} 执行异常: {},joinPoint.getSignature().getName(),ex.getMessage());}
}// 自定义注解
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
public interface LogExecutionTime {
}// 使用示例
Service
public class UserService {LogExecutionTimepublic User findById(Long id) {// 业务逻辑}
}系列的第一部分到此暂告一段落内容包含了Spring的基础介绍和核心特性。
如果你觉得这篇文章有帮助欢迎点赞转发也期待在评论区看到你的想法和建议
咱们下一期见