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

网站开发设计的难点WordPress底部设计

网站开发设计的难点,WordPress底部设计,wordpress作者插件,用点心做点心官方网站前言 时光如白驹过隙,一晃笔者又小半个月没更新了,也许无人在意笔者为何没更新了,但笔者还是想通报一下, 笔者这段时间一方面在备战期末考试#xff0c;另一方面也在梳理学习笔记。 现在#xff0c;决定开启 Spring 系列博客的正式旅程。 本篇将聚焦 Spring 的核心思想之…前言 时光如白驹过隙,一晃笔者又小半个月没更新了,也许无人在意笔者为何没更新了,但笔者还是想通报一下, 笔者这段时间一方面在备战期末考试另一方面也在梳理学习笔记。 现在决定开启 Spring 系列博客的正式旅程。   本篇将聚焦 Spring 的核心思想之一 —— IoC控制反转与 DI依赖注入。希望与大家一同深入探讨让 Spring 的“魔法”不再遥不可及。 为什么我们需要Spring 在深入学习Spring的IoC控制反转和DI依赖注入之前让我们先来看看一个简单的对比你会立刻感受到Spring的魅力。 传统方式 vs Spring方式 传统的Java开发方式 public class OrderController {private OrderService orderService;private PaymentService paymentService;private EmailService emailService;public OrderController() {// 手动创建所有依赖对象this.orderService new OrderService();this.paymentService new PaymentService();this.emailService new EmailService();}public void processOrder(Order order) {orderService.saveOrder(order);paymentService.processPayment(order);emailService.sendConfirmation(order);} } 使用 Spring 框架 RestController public class OrderController {Autowiredprivate OrderService orderService;Autowiredprivate PaymentService paymentService;Autowiredprivate EmailService emailService;PostMapping(/orders)public void processOrder(RequestBody Order order) {orderService.saveOrder(order);paymentService.processPayment(order);emailService.sendConfirmation(order);} } 看起来差别不大让我们继续往下看... 传统开发方式的痛点 1. 对象创建的噩梦 想象一下当你的项目变得复杂时 public class OrderService {private UserRepository userRepository;private ProductRepository productRepository;private InventoryService inventoryService;private DiscountService discountService;private AuditService auditService;public OrderService() {// 每个依赖都需要手动创建this.userRepository new UserRepository();this.productRepository new ProductRepository();this.inventoryService new InventoryService();this.discountService new DiscountService();this.auditService new AuditService();// 但是等等...这些服务可能还有自己的依赖// UserRepository可能需要DataSource// InventoryService可能需要CacheManager// 这会形成一个复杂的依赖链...} } 问题显而易见 代码臃肿构造函数被大量的对象创建代码污染 硬编码依赖如果要更换实现必须修改源代码 测试困难无法轻易mock依赖对象进行单元测试 维护噩梦依赖关系复杂牵一发而动全身 2. 配置地狱 还记得那些冗长的XML配置文件吗? !-- 传统的XML配置数百行的重复配置 -- bean iddataSource classcom.mysql.jdbc.Driverproperty nameurl valuejdbc:mysql://localhost:3306/mydb/property nameusername valueroot/property namepassword valuepassword/ /beanbean iduserRepository classcom.example.UserRepositoryproperty namedataSource refdataSource/ /beanbean idorderService classcom.example.OrderServiceproperty nameuserRepository refuserRepository/property nameproductRepository refproductRepository/!-- ...更多配置... -- /bean 3. 测试的痛苦 // 传统方式下的单元测试 public class OrderServiceTest {Testpublic void testCreateOrder() {// 为了测试OrderService需要创建所有的依赖UserRepository userRepo new UserRepository();ProductRepository productRepo new ProductRepository();InventoryService inventoryService new InventoryService();// ...还有更多依赖OrderService orderService new OrderService();// 测试代码比业务代码还复杂} } Spring带来的革命性改变 现在让我们看看Spring是如何优雅地解决这些问题的 1. 告别手动创建对象 Service public class OrderService {Autowiredprivate UserRepository userRepository;Autowiredprivate ProductRepository productRepository;Autowiredprivate InventoryService inventoryService;// 构造函数变得干净简洁// 所有依赖由Spring自动注入public void createOrder(Order order) {// 专注于业务逻辑而不是对象创建User user userRepository.findById(order.getUserId());// ...业务逻辑} } 2. 配置变得简单 Configuration ComponentScan(com.example) public class AppConfig {// 一个注解胜过千行XML }// 或者更简单使用Spring Boot SpringBootApplication public class Application {// 零配置启动 } 3. 测试变得轻松 ExtendWith(SpringExtension.class) class OrderServiceTest {Mockprivate UserRepository userRepository;InjectMocksprivate OrderService orderService;Testvoid testCreateOrder() {// 轻松mock依赖专注测试业务逻辑when(userRepository.findById(1L)).thenReturn(mockUser);orderService.createOrder(testOrder);verify(userRepository).findById(1L);} } 真实场景模拟一个电商系统的对比 让我们通过一个真实的电商系统场景来感受这种差异 我们假设一个用户下单系统需要 扣减库存 执行支付 返回下单结果 传统方式的订单处理类 // 传统方式自己创建依赖对象 public class OrderService {private PaymentService paymentService;private InventoryService inventoryService;public OrderService() {this.paymentService new PaymentService(); // 主动创建依赖对象this.inventoryService new InventoryService();}public void createOrder() {inventoryService.deductStock();paymentService.pay();System.out.println(订单创建成功);}public static void main(String[] args) {OrderService orderService new OrderService();orderService.createOrder();} }class PaymentService {public void pay() {System.out.println(支付成功);} }class InventoryService {public void deductStock() {System.out.println(库存扣减成功);} }问题 代码耦合严重OrderService 无法方便地更换或测试依赖组件。 不利于维护不利于扩展不易于单元测试。 Spring方式的订单处理类 1. 定义各个组件 import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;Service public class OrderService {Autowiredprivate PaymentService paymentService;Autowiredprivate InventoryService inventoryService;public void createOrder() {inventoryService.deductStock();paymentService.pay();System.out.println(订单创建成功);} }Service class PaymentService {public void pay() {System.out.println(支付成功);} }Service class InventoryService {public void deductStock() {System.out.println(库存扣减成功);} }2. 启动类模拟运行 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext;SpringBootApplication public class ECommerceApp {public static void main(String[] args) {ApplicationContext context SpringApplication.run(ECommerceApp.class, args);OrderService orderService context.getBean(OrderService.class);orderService.createOrder();} }你准备好了吗 看到这里你是否已经感受到了Spring框架的强大魅力它不仅仅是一个工具更是一种编程思想的革命。 Spring解决的不仅仅是技术问题更是开发效率和代码质量的问题 让开发者专注业务逻辑而不是基础设施 让代码更加简洁、优雅、可维护 让测试变得简单提高代码质量 让团队协作更加高效 那么问题来了Spring是如何做到这一切的 答案就在IoC控制反转和DI依赖注入这两个核心概念中。理解了它们你就理解了Spring的灵魂也就掌握了现代Java开发的精髓。 接下来让我们深入探索这个让Java开发变得如此优雅的奥秘吧 结尾: 如果你读到了这里,请不要因为戛然而止感到惊讶,因为笔者想要在下一篇博客中详细介绍 Ioc DI 因此这是引子博客,笔者希望读者明白为什么需要IoC和DI,珍惜Spring带来的便利。
http://www.w-s-a.com/news/636232/

相关文章:

  • 衡水哪有做网站的wordpress主题站主题
  • 网络建设的流程网站公司注册资本
  • 杭州旅游团购网站建设建立一个网站需要哪些步骤
  • 实木餐桌椅网站建设浦东网站建设哪家好
  • 高端手机网站定制网站网络推广推广
  • 做网站的颜色大学网站群建设方案
  • 淄博学校网站建设哪家好网站集约化建设规范
  • 专业论坛网站有哪些如何制作h5页面视频
  • 南京整站优化网站备案负责人一定要法人
  • 北京正规网站建设公司php网站开发实训感想
  • 织梦网站地图怎么做腾讯网站开发语言
  • 站长之家alexa排名wordpress html 标签
  • WordPress建站主机推荐工程公司的经营范围
  • 做网站要注意哪一点网站需求分析的重要
  • 设计作品网站怎么开网站
  • 上海网站开发制作建设网站的建设费用包括
  • 上海网站建设网站开发亚洲杯篮球直播在什么网站
  • 网站做seo第一步h5制作公司
  • 软件外包产业网络优化工程师是干嘛的
  • 怎么用服务器做局域网网站河西网站建设
  • 工业企业网站建设企业门户网站解决方案
  • 网站运营与管理论文网上商城都有哪些
  • 常德网站制作建设毕设电商网站设计
  • 西安企业模板建站福州+网站建设+医疗
  • 邹城市住房和建设局网站仙居网站建设贴吧
  • 为什么要用CGI做网站网站手机优化显示
  • 做袜子娃娃的网站做网站要学的东西
  • 类qq留言网站建设企业做网站公司
  • 如何查到网站建设三足鼎立小程序开发公司
  • 交互网站怎么做的wordpress ssl 错位