示例:spring 纯xml配置
以下是一个完整的 纯 XML 配置开发示例,涵盖 DAO、Service、Controller 层,通过 Spring XML 配置实现依赖注入和事务管理,无需任何注解。
1. 项目结构
src/main/java
├── com.example.dao
│ └── UserDao.java # DAO 接口
│ └── impl
│ └── UserDaoImpl.java # DAO 实现类
├── com.example.service
│ ├── UserService.java # Service 接口
│ └── impl
│ └── UserServiceImpl.java # Service 实现类
└── com.example.controller└── UserController.java # Controller 类
resources
├── applicationContext.xml # Spring 主配置文件
└── db.properties # 数据库配置文件
2. XML 配置 (applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 1. 启用组件扫描(DAO、Service、Controller) --><context:component-scan base-package="com.example"/><!-- 2. 数据源配置(通过 properties 文件注入) --><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"><property name="jdbcUrl" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- 3. JdbcTemplate 配置(用于 DAO 层) --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean><!-- 4. 事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 5. 启用注解驱动的事务管理 --><tx:annotation-driven transaction-manager="transactionManager"/><!-- 6. Service 层事务配置(AOP 方式) --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="serviceMethods" expression="execution(* com.example.service..*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/></aop:config></beans>
3. DAO 层 (UserDao.java 和 UserDaoImpl.java)
接口 (UserDao.java)
package com.example.dao;import com.example.model.User;public interface UserDao {User findById(int id);
}
实现类 (UserDaoImpl.java)
package com.example.dao.impl;import com.example.dao.UserDao;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository("userDao")
public class UserDaoImpl implements UserDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic User findById(int id) {String sql = "SELECT * FROM users WHERE id = ?";return jdbcTemplate.queryForObject(sql, new Object[]{id}, (rs, rowNum) ->new User(rs.getInt("id"), rs.getString("name")));}
}
4. Service 层 (UserService.java 和 UserServiceImpl.java)
接口 (UserService.java)
package com.example.service;import com.example.model.User;public interface UserService {User getUserById(int id);
}
实现类 (UserServiceImpl.java)
package com.example.service.impl;import com.example.dao.UserDao;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service("userService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic User getUserById(int id) {return userDao.findById(id);}
}
5. Controller 层 (UserController.java)
package com.example.controller;import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users/{id}")public User getUser(@PathVariable int id) {return userService.getUserById(id);}
}
6. 模型类 (User.java)
package com.example.model;public class User {private int id;private String name;public User(int id, String name) {this.id = id;this.name = name;}// Getter 和 Setter 省略
}
7. 数据库配置 (db.properties)
jdbc.url=jdbc:mysql://localhost:3306/testdb
jdbc.username=root
jdbc.password=secret
8. 启动应用
主类 (Application.java)
package com.example;import com.example.config.AppConfig;
import com.example.controller.UserController;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) {// 初始化 Spring 上下文ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取 Controller 并测试UserController userController = context.getBean(UserController.class);User user = userController.getUser(1);System.out.println("User: " + user.getName());// 关闭上下文context.close();}
}
9. Spring MVC 配置(web.xml)
<web-app><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
10. 依赖(Maven pom.xml)
<dependencies><!-- Spring 核心 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.20</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.20</version></dependency><!-- 数据库 --><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>5.0.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.20</version></dependency><!-- JSON 序列化 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.3</version></dependency>
</dependencies>
关键点说明
-
纯 XML 配置
- 所有 Bean 定义、依赖注入、事务管理均通过 XML 实现,无需任何注解。
- 使用
<context:component-scan>实现组件扫描,替代@ComponentScan。
-
事务管理
- 通过
<tx:advice>和<aop:config>配置事务属性(传播行为、隔离级别)。 - 在 Service 方法上无需添加
@Transactional。
- 通过
-
数据源和连接池
- 使用 HikariCP 连接池,配置通过
db.properties文件注入。
- 使用 HikariCP 连接池,配置通过
-
Controller 层
- 使用
@RestController和@GetMapping注解(此处假设已通过 XML 配置 Spring MVC)。
- 使用
常见问题排查
-
Bean 未找到
- 检查
<context:component-scan>的包路径是否正确。 - 确保类名与 XML 中定义的 Bean ID 一致(如
userService)。
- 检查
-
事务不生效
- 检查
<tx:advice>是否配置了正确的事务属性。 - 确保
<aop:config>正确指向 Service 层方法。
- 检查
-
数据库连接失败
- 检查
db.properties中的 URL、用户名和密码。 - 确保数据库驱动已添加到依赖(如 MySQL 的
mysql-connector-java)。
- 检查
通过这种方式,可以完全基于 XML 配置实现 Spring 的依赖注入和事务管理,适合需要严格分离配置与代码的遗留项目。
相关文章:
示例:spring 纯xml配置
以下是一个完整的 纯 XML 配置开发示例,涵盖 DAO、Service、Controller 层,通过 Spring XML 配置实现依赖注入和事务管理,无需任何注解。 1. 项目结构 src/main/java ├── com.example.dao │ └── UserDao.java # DAO 接口…...
SQL预编译——预编译真的能完美防御SQL注入吗
SQL注入原理 sql注入是指攻击者拼接恶意SQL语句到接受外部参数的动态SQL查询中,程序本身 未对插入的SQL语句进行过滤,导致SQL语句直接被服务端执行。 拼接的SQL查询例如,通过在id变量后插入or 11这样的条件,来绕过身份验证&#…...
系统架构师2025年论文《论基于UML的需求分析》
论基于构件的软件开发 摘要: 2011 年 3 月,我有幸参加了某市医院预约挂号系统项目的开发工作,并担任系统架构师一职,负责系统的架构设计及核心构件的开发工作。该项目是某市医院为提升患者就医体验、优化挂号流程而委托开发的,项目于 2011 年底验收,满足了医院及患者提…...
运行neo4j.bat console 报错无法识别为脚本,PowerShell 教程:查看语言模式并通过注册表修改受限模式
无法将“D:\neo4j-community-4.4.38-windows\bin\Neo4j-Management\Get-Args.ps1”项识别为cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 前提配置好环境变量之后依然报上面的错…...
【EDA软件】【设计约束和分析操作方法】
1. 设计约束 设计约束主要分为物理约束和时序约束。 物理约束主要包括I/O接口约束(如引脚分配、电平标准设定等物理属性的约束)、布局约束、布线约束以及配置约束。 时序约束是FPGA内部的各种逻辑或走线的延时,反应系统的频率和速度的约束…...
【Lua】Lua 入门知识点总结
Lua 入门学习笔记 本教程旨在帮助有编程基础的学习者快速入门Lua编程语言。包括Lua中变量的声明与使用,包括全局变量和局部变量的区别,以及nil类型的概念、数值型、字符串和函数的基本操作,包括16进制表示、科学计数法、字符串连接、函数声明…...
Godot学习-关于3D模型选择问题
下面是OBJ、glTF/GLB、BLEND和FBX四种3D模型格式的比较表格,以便更直观地了解它们之间的差异: 特性/格式OBJglTF / GLBBLENDFBX文件类型文本文本/二进制二进制二进制几何数据支持支持支持支持材质支持基础高级(PBR等)完整支持高级…...
光谱相机在肤质检测中的应用
光谱相机在肤质检测中具有独特优势,能够通过多波段光谱分析皮肤深层成分及生理状态,实现非侵入式、高精度、多维度的皮肤健康评估。以下是其核心应用与技术细节: 一、工作原理 光谱反射与吸收特性: 血红蛋白&a…...
IDEA 插件推荐清单(2025)
IDEA 插件推荐清单 精选高效开发必备插件,提升 Java 开发体验与效率。 参考来源:十六款好用的 IDEA 插件,强烈推荐!!!不容错过 代码开发助手类 插件名称功能简介推荐指数CodeGeeX智能代码补全、代码生成、…...
机器学习第一篇 线性回归
数据集:公开的World Happiness Report | Kaggle中的happiness dataset2017. 目标:基于GDP值预测幸福指数。(单特征预测) 代码: 文件一:prepare_for_traning.py """用于科学计算的一个库…...
CS144 Lab1实战记录:实现TCP重组器
文章目录 1 实验背景与要求1.1 TCP的数据分片与重组问题1.2 实验具体任务 2 重组器的设计架构2.1 整体架构2.2 数据结构设计 3 重组器处理的关键场景分析3.1 按序到达的子串(直接写入)3.2 乱序到达的子串(需要存储)3.3 与已处理区…...
Linux安装mysql_exporter
mysqld_exporter 是一个用于监控 MySQL 数据库的 Prometheus exporter。可以从 MySQL 数据库的 metrics_schema 收集指标,相关指标主要包括: MySQL 服务器指标:例如 uptime、version 等数据库指标:例如 schema_name、table_rows 等表指标:例如 table_name、engine、…...
BeautifulSoup 库的使用——python爬虫
文章目录 写在前面python 爬虫BeautifulSoup库是什么BeautifulSoup的安装解析器对比BeautifulSoup的使用BeautifulSoup 库中的4种类获取标签获取指定标签获取标签的的子标签获取标签的的父标签(上行遍历)获取标签的兄弟标签(平行遍历)获取注释根据条件查找标签根据CSS选择器查找…...
HTTP的Header
一、HTTP Header 是什么? HTTP Header 是 HTTP 协议中的头部信息部分,位于请求或响应的起始行之后,用来在客户端(浏览器等)与服务器之间传递元信息(meta-data)(简单理解为传递信息的…...
linux虚拟机网络问题处理
yum install -y yum-utils \ > device-mapper-persistent-data \ > lvm2 --skip-broken 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http://mirrorlist.centos.org/?release7&arch…...
unet算法发展历程简介
UNet是一种基于深度学习的图像分割架构,自2015年提出以来经历了多次改进和扩展,逐渐成为医学图像分割和其他精细分割任务的标杆。以下是UNet算法的主要发展历程和关键变体: 1. 原始UNet(2015) 论文: U-Net: Convoluti…...
基于华为云 ModelArts 的在线服务应用开发(Requests 模块)
基于华为云 ModelArts 的在线服务应用开发(Requests 模块) 一、本节目标 了解并掌握 Requests 模块的特点与用法学会通过 PythonRequests 访问华为云 ModelArts 在线推理服务熟悉 JSON 模块在 Python 中的数据序列化与反序列化掌握 Python 文件 I/O 的基…...
【Rust】基本概念
目录 第一个 Rust 程序:猜数字基本概念变量和可变性可变性常量变量隐藏 数据类型标量类型整型浮点型数值运算布尔型字符类型 复合类型元组数组 函数参数语句与表达式函数返回值 控制流使用 if 表达式控制条件if 表达式使用 else if 处理多重条件在 let 语句中使用 i…...
AI-Sphere-Butler之如何使用Llama factory LoRA微调Qwen2-1.5B/3B专属管家大模型
环境: AI-Sphere-Butler WSL2 英伟达4070ti 12G Win10 Ubuntu22.04 Qwen2.-1.5B/3B Llama factory llama.cpp 问题描述: AI-Sphere-Butler之如何使用Llama factory LoRA微调Qwen2-1.5B/3B管家大模型 解决方案: 一、准备数据集我这…...
C++学习之游戏服务器开发十四QT登录器实现
目录 1.界面搭建 2.登录客户端步骤分析 3.拼接登录请求实现 4.发送http请求 5.服务器登录请求处理 6.客户端处理服务器回复数据 7.注册页面启动 8.qt启动游戏程序 1.界面搭建 查询程序依赖的动态库 ldd 程序名 do 1 cdocker rm docker ps -aq 静态编译游戏服务程序&a…...
协同推荐算法实现的智能商品推荐系统 - [基于springboot +vue]
🛍️ 智能商品推荐系统 - 基于springboot vue 🚀 项目亮点 欢迎来到未来的购物体验!我们的智能商品推荐系统就像您的私人购物顾问,它能读懂您的心思,了解您的喜好,为您精心挑选最适合的商品。想象一下&am…...
【LLM】Ollama:容器化并加载本地 GGUF 模型
本教程将完整演示如何在支持多 GPU 的环境下,通过 Docker 实现 Ollama 的本地化部署,并深度整合本地 GGUF 模型。我们将构建一个具备生产可用性的容器化 LLM 服务,包含完整的存储映射、GPU 加速配置和模型管理方案。 前提与环境准备 操作系统…...
实践项目开发-hbmV4V20250407-Taro项目构建优化
Taro项目构建优化实践:大幅提升开发效率 项目背景 在开发基于ReactTaro的前端项目时,随着项目规模的增长,构建速度逐渐成为开发效率的瓶颈。通过一系列构建优化措施,成功将开发环境的构建速度提升了30%-50%,显著改善…...
MySQL中根据binlog日志进行恢复
MySQL中根据binlog日志进行恢复 排查 MySQL 的 binlog 日志问题及根据 binlog 日志进行恢复的方法一、引言二、排查 MySQL 的 binlog 日志问题(一)确认 binlog 是否开启(二)查找 binlog 文件位置和文件名模式(三&#…...
Jenkins的地位和作用
所处位置 Jenkins 是一款开源的自动化服务器,广泛应用于软件开发和测试流程中,主要用于实现持续集成(CI)和持续部署(CD)。它在开发和测试中的位置和作用可以从以下几个方面来理解: 1. 在开发和测…...
【集合】底层原理实现及各集合之间的区别
文章目录 集合2.1 介绍一下集合2.2 集合遍历的方法2.3 线程安全的集合2.4 数组和集合的区别2.5 ArrayList和LinkedList的区别2.6 ArrayList底层原理2.7 LinkedList底层原理2.8 CopyOnWriteArrayList底层原理2.9 HashSet底层原理2.10 HashMap底层原理2.11 HashTable底层原理2.12…...
软考高级-系统架构设计师 论文范文参考(二)
文章目录 论企业应用集成论软件三层结构的设计论软件设计模式的应用论软件维护及软件可维护性论信息系统安全性设计论信息系统的安全性设计(二)论信息系统的架构设计论信息系统架构设计(二) 论企业应用集成 摘要: 2016年9月,我国某省移动通信有限公司决定启动VerisB…...
srp batch
参考网址: Unity MaterialPropertyBlock 正确用法(解决无法合批等问题)_unity_define_instanced_prop的变量无法srp合批-CSDN博客 URP | 基础CG和HLSL区别 - 哔哩哔哩 (bilibili.com) 【直播回放】Unity 批处理/GPU Instancing/SRP Batche…...
【Linux运维涉及的基础命令与排查方法大全】
文章目录 前言1、计算机网络常用端口2、Kali Linux中常用的命令3、Kali Linux工具的介绍4、Ubuntu没有网络连接解决方法5、获取路由6、数据库端口 前言 以下介绍计算机常见的端口已经对应的网络协议,Linux中常用命令,以及平时运维中使用的排查网络故障的…...
【2025最新Java八股】redis中io多路复用怎么回事,和多线程的关系
io多路复用 IO 多路复用和多线程是两种不同的技术,他们都是用于改善程序在处理多个任务或多个数据流时的效率和性能的。 但是他俩要解决的问题不一样!IO多路复用主要是提升I/O操作的效率和利用率,所以适合 IO 密集型应用。多线程则是提升CP…...
