Spring Boot配置轮询报错:IllegalAccessError问题求助
你在Spring Boot应用中配置轮询功能时遇到以下错误:
java.lang.IllegalAccessError: tried to access method org.springframework.integration.context.IntegrationProperties.()V from class org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration
at org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.integrationGlobalProperties(IntegrationAutoConfiguration.java:88)
已添加的依赖:
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-http</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> </dependency>
实现代码:
@InboundChannelAdapter(channel = "channel", poller = @Poller(fixedDelay = "5000")) public String foo() { System.out.println("test1"); return "foo"; } @ServiceActivator(inputChannel = "channel") public void handle(String in) { System.out.println("test"); }
问题原因及解决方法
这个错误本质是Spring Boot与Spring Integration版本不兼容导致的:IntegrationProperties的构造方法在部分版本中为包私有访问权限,但Spring Boot的自动配置类尝试直接调用该方法,从而触发非法访问异常。
具体解决步骤:
改用Spring Boot Starter依赖
放弃单独引入spring-integration-core和spring-integration-http,使用Spring Boot提供的starter依赖,它会自动管理所有兼容的组件版本:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>手动对齐版本(若必须单独指定)
如果需要单独控制Spring Integration版本,必须保证其与Spring Boot版本严格匹配:- Spring Boot 2.7.x 对应 Spring Integration 5.5.x
- Spring Boot 3.x 对应 Spring Integration 6.x
可通过Spring Boot官方的依赖版本映射表确认具体对应关系。
清理依赖缓存
清理本地Maven/Gradle的依赖缓存,避免因缓存中残留的旧版本依赖导致版本冲突。
内容的提问来源于stack exchange,提问作者Dharmin Patel

