spring boot 二十五 @Configuration注解

在spring boot @Configuration注解 替代 原有的 xxxx.xml 配置。

这里以 ActiveMQ 为例:(演示代码,不保证能运行起来。)
我们将 spring 。xml 方式的配置 该为 spring boot中 注解方式的配置:
xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <!-- 这是配置连接activemq的连接工厂 -->
    <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
            <value>tcp://127.0.0.1:61616</value>
        </property>
    </bean>


    <!-- 创建一个jmsTemplate 对象-->
    <bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="destinationName" value="springTopic"/>
        <property name="messageListener" value="messageListener" />
        <property name="pubSubDomain" value="true"/>
    </bean>

    <!-- 这是对activemq 的队列(queue)配置bean-->
    <bean id="messageListener" class="com.example.activemq.listeners.TestListener"/>

</beans>

修改为 @Configration 方法如下:

@Configuration
public class ActivemqConfig {

    /**   
     <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
     <property name="brokerURL">
     <value>tcp://127.0.0.1:61616</value>
     </property>
     </bean>
     ActiveMQConnectionFactory 一般最基础的类,在加入依赖后,spring boot 都会直接帮我们创建出来
     所以,我们这里直接可以使用  Autowired 来获取
     */
    @Autowired
    private ActiveMQConnectionFactory connectionFactory;

    @Value("${spring.jms.template.default-destination}")
    private String destinationName;

    @Value("${spring.jms.pub-sub-domain}")
    private boolean pubSubDomain;
    
       /** 
      <bean id="testListener" class="com.example.activemq.listeners.TestListener"/>
     这个地方的的bean 是自己写的实现类方法,可以直接在类上 加 @Compoent 
     然后通过 @Autowired 形式注入进来
         */
    @Autowired
    private TestListener testListener;


    /**
     <bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
     <property name="connectionFactory" ref="jmsFactory"/>
     <property name="destinationName" value="springTopic"/>
     <property name="messageListener" value="testListener" />
     <property name="pubSubDomain" value="true"/>
     </bean>
     //最后这个地方 配置一个 DefaultMessageListenerContainer  需要设置参数,可以使用下面的方式设置进来。基本 xml 方法能做到的   都可以通过 @Configuration 方式来配置。
     */
    @Bean
    public DefaultMessageListenerContainer defaultMessageListenerContainer(){
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setDestinationName(destinationName);
        container.setMessageListener(testListener);
        container.setPubSubDomain(pubSubDomain);
        return container;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值