eclipse - 如何在 Spring Boot 中使用 Bean 创建配置类?

标签 eclipse spring maven spring-boot

我正在将使用 spring jms 完成的项目转换为 spring boot 项目。我不知道如何将spring jms中的context.xml转换为spring boot中的配置类。我的context.xml如下

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location">
        <value>file:./config-env-receiver.properties</value>
    </property>
</bean>

<bean id="stepConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
    <property name="hostName">
        <value>${mq.hostname.step}</value>
    </property>
    <property name="port">
        <value>${mq.port.step}</value>
    </property>
    <property name="channel">
        <value>${mq.channel.step}</value>
    </property>
    <property name="queueManager">
        <value>${mq.queuemanager.step}</value>
    </property>
    <property name="transportType">
        <value>1</value>
    </property>
</bean>

<bean id="jmsDestination" class="com.ibm.mq.jms.MQQueue">
    <constructor-arg value="${mq.queuename.step}" />
</bean>
<bean
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="stepConnectionFactory" />
    <property name="sessionTransacted" value="true" />
    <property name="destinationName" value="${mq.queuename.step}" />
    <property name="exceptionListener" ref="exceptionListener" />
    <property name="messageListener" ref="stepOutListenerItemCreateUpdate" />
</bean>
<bean id="exceptionListener" class="com.message.view.CustomException">      
</bean>
    <bean id="stepOutListenerItemCreateUpdate"
    class="com.message.view.WMQueueMessageConsumer">
        </bean>
<bean id="springConnectionFactory"
    class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory" ref="stepConnectionFactory" />
</bean>

<bean id="jmsTemplateStep" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="springConnectionFactory" />
    <property name="defaultDestination" ref="jmsDestination" />
</bean>

<context:component-scan base-package="com.message.view">
</context:component-scan>

我尝试按如下方式创建 Application.class。

package hello;
import java.util.Arrays;
import javax.jms.JMSException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnectionFactory;

@SpringBootApplication
//@ImportResource("classpath:context_receiver.xml")
public class Application {

@Autowired
public CustomException customException;

@Autowired
public MessageListener messageListener;

@Bean
public MQQueueConnectionFactory getMQconnectionfactory(){
    MQQueueConnectionFactory mqconfactory=new MQQueueConnectionFactory();

    try {
        mqconfactory.setHostName("*******");
        mqconfactory.setPort(*****);
        mqconfactory.setChannel("**********");
        mqconfactory.setQueueManager("********");
        mqconfactory.setTransportType(1);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mqconfactory;

}

@Bean
public MQQueue getMQQueue(){
    return new MQQueue();
} 



@Bean
public DefaultMessageListenerContainer getDefaultMessageListenerContainer(){
    DefaultMessageListenerContainer defmesliscont=new DefaultMessageListenerContainer();
    defmesliscont.setConnectionFactory(getMQconnectionfactory());
    defmesliscont.setSessionTransacted(true);
    defmesliscont.setDestinationName("********");
    defmesliscont.setExceptionListener(customException);
    defmesliscont.setMessageListener(messageListener);
    return defmesliscont;
}

@Bean
public SingleConnectionFactory getSingleConnectionFactory(){
    SingleConnectionFactory singleConnectionFactory=new SingleConnectionFactory();
    singleConnectionFactory.setTargetConnectionFactory(getMQconnectionfactory());
    return singleConnectionFactory;
}

@Bean
public JmsTemplate getJmsTemplate(){
    JmsTemplate jmsTemplate=new JmsTemplate();
    jmsTemplate.setConnectionFactory(getSingleConnectionFactory());
    jmsTemplate.setDefaultDestination(getMQQueue());
    return jmsTemplate;
}

public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(Application.class, args);

    System.out.println("Let's inspect the beans provided by Spring Boot:");

    String[] beanNames = ctx.getBeanDefinitionNames();
    Arrays.sort(beanNames);
    for (String beanName : beanNames) {
        System.out.println(beanName);
    }
}

}

但我收到此错误。

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: ; nested exception is java.lang.IllegalArgumentException: Attribute 'exclude' is of type [Class[]], but [String[]] was expected. Cause: 
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:383)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:162)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:296)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:240)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at hello.Application.main(Application.java:81)
Caused by: java.lang.IllegalArgumentException: Attribute 'exclude' is of type [Class[]], but [String[]] was expected. Cause: 
at org.springframework.core.annotation.AnnotationAttributes.doGet(AnnotationAttributes.java:117)
at org.springframework.core.annotation.AnnotationAttributes.getStringArray(AnnotationAttributes.java:70)
at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:69)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:379)
... 13 common frames omitted

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: ; nested exception is java.lang.IllegalArgumentException: Attribute 'exclude' is of type [Class[]], but [String[]] was expected. Cause: 
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:383)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:162)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:296)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:240)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at hello.Application.main(Application.java:81)
Caused by: java.lang.IllegalArgumentException: Attribute 'exclude' is of type [Class[]], but [String[]] was expected. Cause: 
at org.springframework.core.annotation.AnnotationAttributes.doGet(AnnotationAttributes.java:117)
at org.springframework.core.annotation.AnnotationAttributes.getStringArray(AnnotationAttributes.java:70)
at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:69)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:379)
... 13 more

我什至尝试使用@ImportResource 导入context.xml。但这也行不通。虽然我更喜欢通过 Application.class 配置它。 请告诉我做错了什么。谢谢。

最佳答案

我已经成功了。这是来自三个工作 pom 文件的片段

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.0.RELEASE</version>
</parent>



<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>

关于eclipse - 如何在 Spring Boot 中使用 Bean 创建配置类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212087/

相关文章:

java - Eclipse Android 插件在 basicHttpBinding 中调用 WCF Web 服务

spring - Reactor/Reactive Kafka 中有 @KafkaListener 吗?

java - 闪屏问题?

eclipse - Eclipse 中的 Maven/AJDT 项目

java - JPA、Spring、Hibernate 加载实体多对多关联的问题

java - 无法反序列化来自 Flurry API 的 json 响应

maven - 通过 maven 将 ActionBar Sherlock 与 intellij 结合使用。 AndroidManifest.xml 中的 @style/Theme.Sherlock 不匹配

java - Scala 与 Maven : Execute a -jar-with-dependencies with a class

java - Sonar 项目与 Sonar 模块( Sonar 项目的一部分)

eclipse - 如何在 Eclipse 中获取 Android API 文档