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

Spring-boot读书笔记一Boilerplate code

Boilerplate code refers to repetitive, verbose code that you have to write over and over again with little variation. It's code that follows the same pattern but doesn't add much business value.

Examples of Boilerplate Code:
1. Traditional DAO (Data Access Object) Pattern:

// WITHOUT Spring Data JPA - LOTS of boilerplate
public class UserDAOImpl implements UserDAO {@Autowiredprivate EntityManager entityManager;public User save(User user) {if (user.getId() == null) {entityManager.persist(user);return user;} else {return entityManager.merge(user);}}public User findById(Long id) {return entityManager.find(User.class, id);}public List<User> findAll() {return entityManager.createQuery("SELECT u FROM User u", User.class).getResultList();}public void delete(User user) {entityManager.remove(entityManager.contains(user) ? user : entityManager.merge(user));}public List<User> findByUsername(String username) {return entityManager.createQuery("SELECT u FROM User u WHERE u.username = :username", User.class).setParameter("username", username).getResultList();}
}

2. WITH Spring Data JPA - NO boilerplate:

// Just this interface - Spring generates everything!
public interface UserRepository extends JpaRepository<User, Long> {List<User> findByUsername(String username);
}

Other Boilerplate Examples:

Getters/Setters:// Boilerplate
public class User {private String name;public String getName() { return name; }public void setName(String name) { this.name = name; }
}// With Lombok - eliminates boilerplate
@Data
public class User {private String name;
}

Exception Handling:

// Boilerplate
try {// some operation
} catch (Exception e) {logger.error("Error occurred", e);throw new ServiceException("Operation failed", e);
}

Configuration:

// Boilerplate XML configuration
<bean id="userService" class="com.example.UserService"><property name="userRepository" ref="userRepository"/>
</bean>// Spring Boot auto-configuration - no boilerplate
@Service
public class UserService {@Autowiredprivate UserRepository userRepository;
}

Why Boilerplate is Bad:

  • Repetitive - same code written multiple times
  • Error-prone - more code = more bugs
  • Maintenance burden - changes require updating many places
  • Reduces productivity - time spent on non-business logic

How Frameworks Reduce Boilerplate:

  • Code generation (Spring Data JPA)
  • Annotations (Lombok, Spring)
  • Convention over configuration (Spring Boot)
  • Auto-configuration (Spring Boot starters)

The goal is to write less code that does more - focusing on business logic rather than infrastructure concerns.

http://www.jsqmd.com/news/176205/

相关文章:

  • 【VSCode身份认证升级方案】:3大步骤完成Entra ID与Azure无缝适配
  • 手把手教你用ReFT进行高效参数微调(附GPU租赁链接)
  • 深度学习环境配置痛点终结者:YOLOv8完整镜像上线
  • Python代码重复检测神器:用Pylint的symilar工具彻底清理你的代码库
  • 终极指南:7大核心功能打造专业级iOS表格组件
  • NaughtyAttributes:Unity开发者的Inspector革命
  • Skyvern:让AI帮你搞定所有重复性网页操作
  • PokeAPI技术指南:构建专业的Pokémon数据服务平台
  • 如何在浏览器中零配置运行完整的JavaScript Linux模拟器?
  • CPT/SFT/DPO/RM全流程打通,端到端训练只需三步
  • Push Notifications推送通知测试终极指南:跨平台解决方案
  • CH340/CH341驱动完整解决方案:5分钟解决Windows串口连接难题
  • GTKWave 3.3.100 Windows 64位版本深度解析
  • 金融支付安全漏洞扫描:软件测试从业者实战指南‌
  • 突破传统:Linux平台3款Markdown编辑器横向测评,Remarkable为何脱颖而出?
  • LoRA+与Adapter融合微调实验成功!详细步骤已开源,附GPU优惠
  • ShopXO开源商城:10分钟快速部署终极指南
  • 边缘AI设备电源保护终极指南:从零构建安全供电系统
  • 如何用SeedVR2-3B实现专业级视频修复:28倍效率提升的完整指南
  • ext4文件系统日志机制终极指南:从数据安全到性能调优
  • AirConnect:让普通音响秒变AirPlay设备的完整指南
  • 神马搜索移动适配:确保手机用户顺利找到DDColor服务
  • 儿童节彩蛋上线!AI讲故事模式吸引年轻用户
  • PyCharm激活码永不过期?不如看看这个能跑Llama3的GPU云实例
  • Cardinal虚拟模块合成器:音乐创作者的终极数字实验室
  • github trending榜单遗漏了什么?这个AI工具连续霸榜三周
  • EcMenu:免费右键菜单管理神器,轻量绿色无捆绑,右键菜单自定义!
  • ‌智能物业管理系统用户场景测试框架与实战指南
  • RuoYi-App多端开发终极指南:5步快速上手的完整教程
  • 网盘直链下载助手失效?用这个脚本一键拉取HuggingFace模型(附GPU链接)