gpt4 book ai didi

spring-boot - 使用 spring-JMS 和 spring cloud sleuth 时的 BeanNotOfRequiredTypeException

转载 作者:行者123 更新时间:2023-12-05 03:03:36 26 4
gpt4 key购买 nike

我有带有 Spring Cloud sleuth 的 spring boot 项目(2.1.0 版本)。Spring Sleuth 的 TracingConnectionFactoryBeanPostProcessor 正在使用 LazyConnectionFactory Bean 覆盖 ConnectionFactory bean。因此,我正在点击:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userCredentialsConnectionFactoryAdapter' is expected to be of type 'org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter' but was actually of type 'org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory'

任何人都可以为此提出解决方案/解决方法。

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sleuth.test</groupId>
<artifactId>Jms-Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Jms-Test</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>


</project>

.

@Configuration
public class JmsConfig {

@Bean
public ActiveMQConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp://localhost:61616");
connectionFactory.setPassword("admin");
connectionFactory.setUserName("admin");
return connectionFactory;
}

@Bean
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(ConnectionFactory conFact) {
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
userCredentialsConnectionFactoryAdapter.setUsername("admin");
userCredentialsConnectionFactoryAdapter.setPassword("admin");
userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(conFact);
return userCredentialsConnectionFactoryAdapter;
}

@Bean
public JmsTemplate jmsTemplate(UserCredentialsConnectionFactoryAdapter conFactory){
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(conFactory);
return template;
}

@SpringBootApplication
public class JmsTestApplication {

@Autowired(required=true)
JmsTemplate jmsTemplate;

public static void main(String[] args) {
SpringApplication.run(JmsTestApplication.class, args);
new JmsTestApplication().sendMessage();

}

private void sendMessage() {
// TODO Auto-generated method stub
jmsTemplate.convertAndSend("QUEUE.USER", "user");

}

最佳答案

>@Bean
>public JmsTemplate jmsTemplate(UserCredentialsConnectionFactoryAdapter conFactory){

不要注入(inject)具体类型;而是注入(inject)接口(interface)

@Bean
public JmsTemplate jmsTemplate(ConnectionFactory conFactory){

并在必要时使用 @Qualifier 对其进行限定,或者使用正确的 bean 名称作为参数名称。

关于spring-boot - 使用 spring-JMS 和 spring cloud sleuth 时的 BeanNotOfRequiredTypeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53683964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com