使用spring-JMS和spring cloud slueth时的BeanNotOfRequiredTypeException

时间:2018-12-08 15:25:20

标签: spring-boot spring-jms spring-cloud-sleuth

我有使用Spring Cloud sleuth的spring boot项目(2.1.0版本)。 Spring Sleuth的TracingConnectionFactoryBeanPostProcessor用LazyConnectionFactory Bean覆盖ConnectionFactory Bean。结果,我遇到了:

  

org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为“ userCredentialsConnectionFactoryAdapter”的Bean应该是“ org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter”类型,但实际上是“ 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");

    }

1 个答案:

答案 0 :(得分:0)

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

不要注入具体类型;注入界面

@Bean
public JmsTemplate jmsTemplate(ConnectionFactory conFactory){

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