开发基础知识 - Spring的应用结构
Content
以一个基于Dubbo,MyBatis的后台服务框架为例:
File structure:
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实体类
// User.java
package com.example.api.entity;
public class User {
private Long id;
private String name;
private Integer age;
// getters and setters
}接口
// 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接口
// 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映射文件
<?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](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>服务实现类
// 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配置文件
<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配置文件
<beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"
xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)"
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"
xmlns:dubbo="[http://dubbo.apache.org/schema/dubbo](http://dubbo.apache.org/schema/dubbo)"
xsi:schemaLocation="
[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans.xsd](http://www.springframework.org/schema/beans/spring-beans.xsd)
[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context) [http://www.springframework.org/schema/context/spring-context.xsd](http://www.springframework.org/schema/context/spring-context.xsd)
[http://dubbo.apache.org/schema/dubbo](http://dubbo.apache.org/schema/dubbo) [http://dubbo.apache.org/schema/dubbo/dubbo.xsd](http://dubbo.apache.org/schema/dubbo/dubbo.xsd)">
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.api.mapper"/>
</bean>
<bean id="userService" class="com.example.api.service.implementation.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>
<import resource="dubbo-provider.xml"/>
</beans>.properties文件
.properties文件是一种键值对格式的文本文件。比如:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
logging.level.root=INFO- 配置文件放在指定路径(比如 src/main/resources 目录下)
- 启动 Spring 应用时,Spring 会自动读取这些配置文件
- 利用 Spring 的 @Value 或 @ConfigurationProperties 注解,就能把配置值“注入”到 Java 代码里
比如:
// application.properties: app.title=xxx
@Value("${app.title}")
private String title;当程序运行时,Spring 会把 app.title 的值 “我的Spring网站” 注入到这个 title 变量里。
开发基础知识 - Spring的应用结构
http://example.com/2025/07/13/SWE-basis/Spring/spring-architecture/