由Mybatis发现的一个坑

本文介绍了解决@Configuration类中BeanFactoryPostProcessor与@Autowired等注解生命周期冲突的方法。通过允许@Bean方法使用static修饰符,容器可以在不实例化配置类的情况下调用这些方法,避免了生命周期问题,并详细说明了static@Bean方法的限制。

源码很重要

Thanks for the report, Osvaldas. This has indeed been an issue known about, at least internally, for some time.

There is a fundamental lifecycle conflict in handling BeanFactoryPostProcessor @Bean methods within @Configuration classes that use @Autowired, @PostConstruct, @Value, etc. Because BFPPs must be instantiated early in the lifecycle, they cause early instantiation of their declaring @Configuration class - too early to recieve the usual post-processing by AutowiredAnnotationBeanPostProcessor and friends.

To resolve this issue, it is now possible to declare @Bean methods as static. This permits the container to detect and invoke these methods without instantiating the @Configuration class, thus avoiding the aforementioned lifecycle issues.

@Bean Javadoc has been updated to reflect, and the commit comment follows:


Author: Chris Beams <cbeams@vmware.com>
Date:   Tue May 10 18:17:26 2011 +0800
 
    Allow static modifier on @Bean methods
    
    Declaring @Bean methods as 'static' is now permitted, whereas previously
    it raised an exception at @Configuration class validation time.
    
    A static @Bean method can be called by the container without requiring
    the instantiation of its declaring @Configuration class. This is
    particularly useful when dealing with BeanFactoryPostProcessor beans,
    as they can interfere with the standard post-processing lifecycle
    necessary to handle @Autowired, @Inject, @Value, @PostConstruct and
    other annotations.
    
    static @Bean methods cannot recieve CGLIB enhancement for scoping and
    AOP concerns. This is acceptable in BFPP cases as they rarely if ever
    need it, and should not in typical cases ever be called by another
    @Bean method.  Once invoked by the container, the resulting bean will
    be cached as usual, but multiple invocations of the static @Bean method
    will result in creation of multiple instances of the bean.
    
    static @Bean methods may not, for obvious reasons, refer to normal
    instance @Bean methods, but again this is not likely a concern for BFPP
    types. In the rare case that they do need a bean reference, parameter
    injection into the static @Bean method is technically an option, but
    should be avoided as it will potentially cause premature instantiation
    of more beans that the user may have intended.
    
    Note particularly that a WARN-level log message is now issued for any
    non-static @Bean method with a return type assignable to BFPP.  This
    serves as a strong recommendation to users that they always mark BFPP
    @Bean methods as static.
    
    See @Bean Javadoc for complete details.
    
    Issue: SPR-8257, SPR-8269

<think>嗯,用户这次想了解tk.mybatismybatis版本的兼容性问题。从ta提供的引用内容看,之前已经接触过tk.mybatis的使用方法、Devtools冲突问题以及SpringBoot版本兼容问题,说明ta应该是个正在整合框架的开发者。 用户直接问版本兼容性,但引用里并没有明确给出对应关系表。不过从引用[1]和[2]的依赖版本可以推测:ta使用的mapper-spring-boot-starter 2.0.2对应mapper核心包4.0.4,这是较新的组合。需要提醒ta注意MyBatis 3.5.x以上版本才适配。 考虑到tk.mybatis已经停止更新,应该警告ta这个风险。用户可能还隐含了"要不要换其他框架"的疑问,毕竟引用[3]提到过SpringBoot整合问题。需要准备替代方案建议,比如MyBatis-Plus。 用户上次问过依赖冲突(引用[3]),这次要特别强调版本管理工具的重要性。ta遇到Devtools问题(引用[2])也说明环境配置经验不足,文档链接必须给最新可靠的。 啊,发现tk.mybatis官网文档链接失效了,得用GitHub的仓库地址替代。用户可能已经踩过文档的,所以额外提供社区讨论入口比较稳妥。 </think>以下是关于 tk.mybatisMyBatis 版本兼容性的详细解答,综合引用内容和官方实践指南: --- ### 一、核心版本兼容性对照表 根据 tk.mybatis 官方文档和主流实践,推荐组合如下: | tk.mybatis 版本 | MyBatis 版本 | Spring Boot 版本 | 关键特性 | |------------------|---------------------|-------------------|----------------------------------------| | `mapper-4.x.x` | **MyBatis 3.5.0+** | Spring Boot 2.x | 支持 Java 8+ 特性,Lambda 表达式 | | `mapper-3.5.x` | MyBatis 3.4.6 | Spring Boot 1.5.x | 兼容 JDK 1.7,注解式通用 Mapper | | `mapper-2.x.x` | MyBatis 3.4.5 以下 | Spring Boot 1.4.x | 旧版兼容模式 | > **重要提示**: > tk.mybatis `mapper-spring-boot-starter` 的版本需与核心包 `mapper` 版本匹配(如引用[1]中 `mapper-spring-boot-starter:2.0.2` 对应 `mapper:4.0.4`) --- ### 二、常见兼容性问题解决方案 #### 1. **类加载失败问题**(对应引用[2]) ```java tk.mybatis.mapper.MapperException: 无法获取实体类 xxx.UserDO ``` - **原因**:Devtools 的热部署机制会重置 ClassLoader,导致 tk.mybatis 缓存失效[^2]。 - **解决**:在 `application.properties` 中添加: ```properties spring.devtools.restart.enable=false ``` #### 2. **Spring Boot 2.x 兼容问题**(对应引用[3]) ```java java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver ``` - **原因**:tk.mybatis 旧版本依赖 `spring-boot-starter-jdbc:1.x`,其调用的 `RelaxedPropertyResolver` 在 Spring Boot 2.x 已移除[^3]。 - **解决**:升级 tk.mybatis 至 `4.x` 版本,或显式排除旧依赖: ```xml <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </exclusion> </exclusions> </dependency> ``` --- ### 三、版本选择建议 1. **新项目**:直接使用最新版组合 ```xml <!-- MyBatis 3.5.13 + tk.mybatis 4.2.5 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>4.1.5</version> </dependency> ``` 2. **旧项目迁移**: - 若使用 MyBatis 3.4.x,选择 `mapper-3.5.1`(最后支持 Java 7 的版本) - 升级后需检查实体类注解(如 `@Table`、`@Id`)是否符合新规范[^1]。 --- ### 四、官方资源指引 - [tk.mybatis GitHub Wiki](https://github.com/abel533/Mapper/wiki) - [MyBatis 官方版本日志](https://blog.mybatis.org) > **注意**:tk.mybatis 已于 2022 年停止维护,建议新项目考虑迁移至 **MyBatis-Plus** 等活跃框架。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值