chore(config): 更新日志配置文件路径并添加代码风格配置
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
空检查
|
||||
@Null 验证对象是否为null
|
||||
@NotNull 验证对象是否不为null, 无法查检长度为0的字符串
|
||||
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
|
||||
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
|
||||
|
||||
Booelan检查
|
||||
@AssertTrue 验证 Boolean 对象是否为 true
|
||||
@AssertFalse 验证 Boolean 对象是否为 false
|
||||
|
||||
长度检查
|
||||
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内
|
||||
@Length(min=, max=) 验证注解的元素值长度在min和max区间内
|
||||
|
||||
日期检查
|
||||
@Past 验证 Date 和 Calendar 对象是否在当前时间之前
|
||||
@Future 验证 Date 和 Calendar 对象是否在当前时间之后
|
||||
@Pattern 验证 String 对象是否符合正则表达式的规则
|
||||
|
||||
数值检查, 建议使用在Stirng,Integer类型, 不建议使用在int类型上, 因为表单值为“”时无法转换为int, 但可以转换为Stirng为"",Integer为null
|
||||
@Min 验证 Number 和 String 对象是否大等于指定的值
|
||||
@Max 验证 Number 和 String 对象是否小等于指定的值
|
||||
@DecimalMax 被标注的值必须不大于约束中指定的最大值. 这个约束的参数是一个通过BigDecimal定义的最大值的字符串表示.小数存在精度
|
||||
@DecimalMin 被标注的值必须不小于约束中指定的最小值. 这个约束的参数是一个通过BigDecimal定义的最小值的字符串表示.小数存在精度
|
||||
@Digits 验证 Number 和 String 的构成是否合法
|
||||
@Digits(integer=,fraction=) 验证字符串是否是符合指定格式的数字, interger指定整数精度, fraction指定小数精度。
|
||||
@Range(min=, max=) 验证注解的元素值在最小值和最大值之间
|
||||
@Range(min=10000,max=50000,message="range.bean.wage")
|
||||
private BigDecimal wage;
|
||||
@Valid 递归的对关联对象进行校验, 如果关联对象是个集合或者数组,那么对其中的元素进行递归校验,如果是一个map,则对其中的值部分进行校验.(是否进行递归验证)
|
||||
@CreditCardNumber信用卡验证
|
||||
@Email 验证是否是邮件地址, 如果为null,不进行验证, 算通过验证。
|
||||
@ScriptAssert(lang= ,script=, alias=)
|
||||
@URL(protocol=,host=, port=,regexp=, flags=) 地址校验
|
||||
@@ -0,0 +1,8 @@
|
||||
操作类型 | 示例方法名 | 请求路径 | 说明 |
|
||||
|--------|-------------------|--------------------|------------------|
|
||||
| 查询单个 | findTemplate | /template/find | 替换原来的 getXxxById
|
||||
查询列表 | listTemplates | /template/list | 分页查询或条件查询
|
||||
新增 | createTemplate | /template/create | 替换原来的 saveXxx
|
||||
更新 | updateTemplate | /template/update | 替换原来的 modifyXxx
|
||||
删除 | deleteTemplate | /template/delete | 替换原来的 removeXxx
|
||||
特殊操作 | exportTemplate | /template/export | 如导出、下载等
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 68 KiB |
@@ -0,0 +1,131 @@
|
||||
# 关于在Spring Service中使用`this`调用方法的问题
|
||||
|
||||
确实,在Spring管理的Service类中直接使用`this`调用方法会导致Spring的AOP增强失效,这是一个需要注意的重要问题。
|
||||
|
||||
## 问题本质
|
||||
|
||||
当你在Spring Service中这样写代码:
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class MyService {
|
||||
|
||||
public void methodA() {
|
||||
this.methodB(); // 这里使用this调用
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void methodB() {
|
||||
// 数据库操作
|
||||
}
|
||||
}
|
||||
|
||||
@Service
|
||||
public class MyService {
|
||||
public void methodA() {
|
||||
methodB(); // 这里隐式使用了this.methodB()
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void methodB() {
|
||||
// 数据库操作
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`methodB()`上的`@Transactional`注解会失效,因为:
|
||||
|
||||
1. Spring的AOP(包括事务管理`@Transactional`、缓存`@Cacheable`等)是通过代理实现的
|
||||
2. 使用`this`调用会绕过Spring的代理机制,直接调用原始对象的方法
|
||||
3. 只有通过代理调用的方法才会被Spring增强
|
||||
|
||||
## 解决方案
|
||||
|
||||
### 1. 自我注入(推荐)
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class MyService {
|
||||
|
||||
@Autowired
|
||||
private MyService self; // 注入自身代理
|
||||
|
||||
public void methodA() {
|
||||
self.methodB(); // 通过代理调用
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void methodB() {
|
||||
// 数据库操作
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 通过ApplicationContext获取代理
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class MyService implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void methodA() {
|
||||
context.getBean(MyService.class).methodB();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void methodB() {
|
||||
// 数据库操作
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 调整方法结构(最佳实践)
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class MyService {
|
||||
|
||||
private final OtherService otherService;
|
||||
|
||||
public MyService(OtherService otherService) {
|
||||
this.otherService = otherService;
|
||||
}
|
||||
|
||||
public void methodA() {
|
||||
// 将需要事务的方法提取到另一个Service
|
||||
otherService.methodB();
|
||||
}
|
||||
}
|
||||
|
||||
@Service
|
||||
public class OtherService {
|
||||
|
||||
@Transactional
|
||||
public void methodB() {
|
||||
// 数据库操作
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 为什么会出现这种情况?
|
||||
|
||||
Spring的AOP实现原理:
|
||||
1. 默认使用JDK动态代理(基于接口)或CGLIB(基于子类)
|
||||
2. 代理对象包装了原始对象
|
||||
3. 外部调用先经过代理,代理再决定是否应用增强逻辑
|
||||
4. `this`引用指向的是原始对象而非代理对象
|
||||
|
||||
## 最佳实践建议
|
||||
|
||||
1. 尽量避免在Service内部方法间调用需要AOP增强的方法
|
||||
2. 如果必须调用,使用自我注入的方式
|
||||
3. 考虑将需要事务管理的方法拆分到单独的Service中
|
||||
4. 对于复杂的业务逻辑,考虑使用领域驱动设计(DDD)的模式
|
||||
|
||||
这种设计是Spring AOP的固有特性,理解这一点对于正确使用Spring的事务管理、缓存、安全等AOP功能非常重要。
|
||||
@@ -0,0 +1,13 @@
|
||||
Copyright 2021-2025 Odboy
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1 @@
|
||||
一次迭代=N次尝试(流水线实例) + 发布分支(git分支)
|
||||
@@ -0,0 +1,3 @@
|
||||
### 注意事项
|
||||
|
||||
long、bigdecimal返回前端时,字段尽量设置为String,否则会出现精度丢失问题
|
||||
@@ -0,0 +1,27 @@
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Cacheable(value = "users", key = "#userId")
|
||||
public User getUserById(Long userId) {
|
||||
return userRepository.findById(userId).orElse(null);
|
||||
}
|
||||
|
||||
@CachePut(value = "users", key = "#user.id")
|
||||
// @CachePut(value = "users", key = "#user.id + '_' + #user.age")
|
||||
public User updateUser(User user) {
|
||||
userRepository.save(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@CacheEvict(value = "users", key = "#userId")
|
||||
public void deleteUser(Long userId) {
|
||||
userRepository.deleteById(userId);
|
||||
}
|
||||
|
||||
@CacheEvict(value = "users", allEntries = true)
|
||||
public void clearCache() {
|
||||
// 清空缓存
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user