【问题标题】:How to create Configuration class with Beans in spring boot?如何在 Spring Boot 中使用 Beans 创建配置类?
【发布时间】:2015-05-26 13:14:12
【问题描述】:

我正在将使用 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 配置它。 请告诉我做错了什么。谢谢。

【问题讨论】:

  • 看起来它不像您应用中某处的 GET 注释?你有吗?
  • 没有。我不认为我有。我在声明 Beans 对吗?我不确定这是否是为每个 bean 设置属性的方法。
  • 你的代码中有exclude吗?
  • 没有。我哪儿都没有。

标签: eclipse spring maven spring-boot


【解决方案1】:

我也有同样的问题。 看起来问题在于作为 SpringBootApplication 和 https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java 的一部分的 EnableAutoConfiguration 不兼容。 EnableAutoConfiguration 已排除为 Class [],但 AnnotationAttributes 需要 String []。很可能版本不兼容。不过我还没想出解决办法。

【讨论】:

    【解决方案2】:

    我已经开始工作了。这是来自工作 pom 文件的 sn-p

    <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>
    

    【讨论】:

    • 抱歉忘记了属性 4.1.4.RELEASE
    【解决方案3】:

    将你的 spring 版本升级到 4.1.5.RELEASE

    【讨论】:

      【解决方案4】:
      1. 关闭想法

      2. 重新生成idea配置文件

      $ mvn idea:clean
      $ mvn 想法:想法

      1. 重新导入项目

      2. 在idea中再次运行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-10
        • 2019-03-08
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        相关资源
        最近更新 更多