如何在Spring启动时使用Beans创建Configuration类?

时间:2015-03-23 13:55:41

标签: 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配置它。 请告诉我我做错了什么。感谢。

4 个答案:

答案 0 :(得分:2)

已经让它发挥作用了。这是工作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>

答案 1 :(得分:1)

将spring版本升级到4.1.5.RELEASE

答案 2 :(得分:0)

我遇到了同样的问题。 看起来问题在于EnableAutoConfiguration的不兼容性,它是SpringBootApplication和https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java的一部分。 EnableAutoConfiguration已排除为Class [],但AnnotationAttributes需要String []。很可能版本不兼容。我虽然没有提出解决方案。

答案 3 :(得分:0)

  1. 关闭主意

  2. 重新生成构思配置文件

  3.   

    $ mvn idea:clean
      $ mvn idea:idea

    1. 重新进口项目

    2. 再次在想法中运行