开发基础知识 - Spring的应用结构

Content

以一个基于Dubbo,MyBatis的后台服务框架为例:

File structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
api/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── api/
│ │ │ ├── entity/
│ │ │ │ └── User.java
│ │ │ ├── mapper/
│ │ │ │ └── UserMapper.java
│ │ │ ├── service/
│ │ │ │ ├── UserService.java
│ │ │ │ └── implementation/
│ │ │ │ └── UserServiceImpl.java
│ │ └── resources/
│ │ ├── applicationContext.xml
│ │ ├── dubbo-provider.xml
│ │ └── mapper/
│ │ └── UserMapper.xml
└── pom.xml

实体类

1
2
3
4
5
6
7
8
9
10
// User.java
package com.example.api.entity;

public class User {
private Long id;
private String name;
private Integer age;

// getters and setters
}

接口

1
2
3
4
5
6
7
8
9
10
// UserService.java
package com.example.api.service;

import com.example.api.entity.User;
import java.util.List;

public interface UserService {
User getUserById(Long id);
List<User> listUsers();
}

MyBatis接口

1
2
3
4
5
6
7
8
9
10
// UserMapper.java
package com.example.api.mapper;

import com.example.api.entity.User;
import java.util.List;

public interface UserMapper {
User selectUserById(Long id);
List<User> selectAllUsers();
}

MyBatis映射文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- UserMapper.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.api.mapper.UserMapper">

<select id="selectUserById" resultType="com.example.api.entity.User">
SELECT id, name, age FROM user WHERE id = #{id}
</select>

<select id="selectAllUsers" resultType="com.example.api.entity.User">
SELECT id, name, age FROM user
</select>

</mapper>

服务实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// UserServiceImpl.java
package com.example.api.service.implementation;

import com.example.api.entity.User;
import com.example.api.mapper.UserMapper;
import com.example.api.service.UserService;
import java.util.List;

public class UserServiceImpl implements UserService {

private UserMapper userMapper;

// Spring 注入 setter
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}

@Override
public User getUserById(Long id) {
return userMapper.selectUserById(id);
}

@Override
public List<User> listUsers() {
return userMapper.selectAllUsers();
}
}

Dubbo配置文件

1
2
3
4
5
6
<!-- dubbo-provider.xml -->
<dubbo:application name="user-provider"/>
<dubbo:registry address="N/A"/> <!-- 如果没有注册中心 -->
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.example.api.service.UserService"
ref="userService"/>

Spring配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- 加载MyBatis配置 -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
</bean>

<!-- 配置 MyBatis 的 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

<!-- 自动扫描 com.example.api.mapper 包下的接口(Mapper),并为每个接口生成代理对象,交由 Spring 容器管理。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.api.mapper"/>
</bean>

<!-- 注册 UserServiceImpl 到 Spring 容器中,并注入 userMapper -->
<bean id="userService" class="com.example.api.service.implementation.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>

<import resource="dubbo-provider.xml"/>

</beans>

.properties文件

.properties文件是一种键值对格式的文本文件。比如:

1
2
3
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
logging.level.root=INFO
  1. 配置文件放在指定路径(比如 src/main/resources 目录下)
  2. 启动 Spring 应用时,Spring 会自动读取这些配置文件
  3. 利用 Spring 的 @Value 或 @ConfigurationProperties 注解,就能把配置值“注入”到 Java 代码里

比如:

1
2
3
4
// application.properties: app.title=xxx

@Value("${app.title}")
private String title;

当程序运行时,Spring 会把 app.title 的值 “我的Spring网站” 注入到这个 title 变量里。


开发基础知识 - Spring的应用结构
https://thiefcat.github.io/2025/07/13/SWE-basic/spring/spring-architecture/
Author
小贼猫
Posted on
July 13, 2025
Licensed under