You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ创建JmsTemplate Bean时提示符号无法解析问题求助

解决IntelliJ中JmsTemplate无法解析的编译错误

问题根源

编译报错找不到org.springframework.jms.core.JmsTemplate,核心是依赖版本冲突+依赖声明混乱:

  • Spring Boot 2.2.6.RELEASE配套的Spring框架版本是5.2.5.RELEASE,但你手动引入了Spring 6.x的spring-jms:6.0.3,跨大版本的API不兼容,直接导致类找不到。
  • 重复声明了activemq-client(5.15.12和5.16.0两个版本),依赖解析时会出现歧义。
  • spring-boot-starter-test已经包含Spring JMS相关测试依赖,无需单独重复引入。

修复步骤

  1. 统一依赖版本,用Spring Boot Starter管理JMS依赖
    删掉手动指定的spring-jms和重复的activemq-client,改用spring-boot-starter-activemq,它会自动匹配Spring Boot版本对应的Spring JMS和ActiveMQ客户端版本,彻底避免冲突。

  2. 清理后的build.gradle依赖配置

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-data-jpa:2.2.6.RELEASE"
    // 用Spring Boot ActiveMQ Starter自动管理依赖版本
    implementation "org.springframework.boot:spring-boot-starter-activemq:2.2.6.RELEASE"
    implementation "org.codehaus.groovy:groovy-all:3.0.9"
    implementation "org.codehaus.groovy:groovy-yaml:3.0.9"
    implementation "org.apache.httpcomponents:httpclient:4.5.12"

    implementation "com.oracle.database.jdbc:ojdbc10:19.3.0.0"

    testImplementation "org.spockframework:spock-core:2.0-M2-groovy-3.0"
    testImplementation "org.spockframework:spock-spring:2.0-M2-groovy-3.0"
    testImplementation "com.github.tomakehurst:wiremock-jre8:2.35.0"

    testImplementation "org.springframework.boot:spring-boot-starter-test:2.2.6.RELEASE"
    testImplementation 'org.slf4j:slf4j-api:1.7.29'

    implementation group: 'javax.jms', name: 'javax.jms-api', version: '2.0.1'

    implementation 'com.mashape.unirest:unirest-java:1.4.9'
    implementation 'com.google.code.findbugs:jsr305:3.0.2'
}
  1. 修正代码细节
    补全@EnableJms的正确导入,Groovy中无需显式写public修饰符:
package automation.test

import org.apache.activemq.ActiveMQConnectionFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.SpringBootConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.jms.annotation.EnableJms
import org.springframework.jms.core.JmsTemplate

import javax.jms.ConnectionFactory

@SpringBootConfiguration
@EnableJms
class TestConfig {
    @Value('${activemq.host}')
    private String brokerUrl

    @Bean
    ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory()
        activeMQConnectionFactory.setBrokerURL(brokerUrl)
        return activeMQConnectionFactory
    }

    @Bean
    JmsTemplate jmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory())
        jmsTemplate.setReceiveTimeout(10000)
        return jmsTemplate
    }
}
  1. 刷新依赖并编译
    在项目根目录执行终端命令:
./gradlew clean build --refresh-dependencies

内容的提问来源于stack exchange,提问作者raddic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 20:20:36