Spring + XML (新项目)

187 阅读2分钟

最近到了新组, 项目是spring5+xml configuration做的, 项目很好, 有很多可以借鉴的。 这里先记录一些知识

Spring Bean

配置方法

xml中显式配置

<context:component-scan base-package=”xx.class”/>
<bean id="" class="" />

java中显示配置

(@Component)

隐式的bean发现机制和自动装配

(Component Scanning, Autowiring)

 

测试去使用一个config


@ContextConfiguration(classes=CDConfig.class)

public class CDtest{

    @AutoWired

    Private Compact cd;

}

spring为上下文中所有的bean,为组件给定一个ID,将类名中的第一个字母变为小写。

重命名beanId

@Component(“lonelyClub”)

Public class SetPeper{}

或者

@Named(“londly”)

 

自动装配

当spring创建bean的时候, 会通过这个构造器来进行实例化并且传入一个可配置的bean参数。


@Component

Public class CDPlayer{
    Private Compact cd;
    @AutoWired
    Public CDPlayer(Compact cd){
        This.cd=cd;
    }
}

还可以用到setter/insert any method

假如有且只有一个bean匹配依赖需求的话, 那么这个bean将会被装配。

如果没有, spring会跑出一个异常。 可以加入@AutoWired(required=false)

就不会报错, 但是如果没有做null检查的话,会有问题。

可以替换成@Inject

显式注解

JavaConfig

@Configration

Public class CDConfig{}//配置类应该仅包含如何创建bean的细节。

Bean(name=”ss”)

 

<bean>
<constructor-arg value=”s” />
<constructor-arg>
<list>
<value>sds</value>
</list>
</constructor-arg>
</bean>

set重复的会去掉

 

高级装配

@profile(“dev”)

Soring.profiles.active是那个bean被激活

测试的是@ActiveProfiles

条件话的bean


@Bean
@Conditional(MagicExistsCondition.class)
Public MagicBean magicNean(){
    return new MafgicBran();//条件化的创建bean
}

Public class MagicExistsCondition implements Condition{
    Public boolean matches(ConfiitonCOntext context, AnnotatedTypeMetadata metadata){
        Environment env= context.getEnvironment();
        Return env.containsProperty(“magic”);
    }
}

//处理自动装配的歧义性

@Primary

@Component

Public class IceCream implements Dessert {}

<bean id=”iceCream” class=”IceCream” primary=”true”>

 

@Qualifier

然后用了spring-integration做项目解偶

Spring integration

什么是spring-integration

spring-integration类似与企业集成模式,便捷的事业驱动框架用来做系统之间的消息传递的。很方便做模块间的无侵入调用

为什么

组件之间应该是松散的,模块易测的

扩展节点应该有更好的抽象和可以再使用的能力

如果系统徐亚database交互, 监听TCP/UDP等, 或者文件的转移。

如何

A. Message -> (Header, Payload)

B. MessageChannel

有以下集中:PublishSubscribeChannel/QueueChannel/DirectChannel/ExecutorChanel

C. Endpoint

  1. Service Activator 激活器和适配器都可以作为一个消息出通道的节点

  2. Channel Adapter ->通常情况下,通道适配器将在消息与从其他系统(文件,HTTP请求,JMS消息等)接收或发送的任何对象或资源之间进行映射

  3. channel Bridge -> 通道桥梁,用来作为管道之间进行通信使用的,常用情景为:在一个输入管道,将管道的内容发送到另外N个管道输出

例子

1) channel

    <!-- 扫描位置 -->
    <context:component-scan base-package="com.example.demo"/>
<!--    <bean name="serviceImpl1" class="com.example.demo.MainTest.ServiceImpl" />-->
    <int:channel id="testChannel" />
<!--    <int:service-activator input-channel="testChannel" ref="serviceImpl1" method="hello"/>-->
    <int:service-activator input-channel="testChannel" expression="@serviceImpl1.helloWithParam(payload.name,payload.age)"/>
</beans>
<!--beanname 1) ref=<bean name> 2) component ("name")-->
<!--only use once-->
  1. gateway+Channel Adapter
<context:component-scan base-package="com.example.demo"/>
<int:channel id="getWayChannel">
    <int:queue/>
</int:channel>
<int:gateway service-interface="com.example.demo.MainTest.UseSender" id="getWatHello" default-request-channel="getWayChannel"/>
<int:outbound-channel-adapter channel="getWayChannel" ref="serviceImpl1" method="hello">
    <int:poller fixed-delay="0">
    </int:poller>
</int:outbound-channel-adapter>
<!--        getWayChannel 不能jiao input-->
  1. router (按照header)
<context:component-scan base-package="com.example.demo"/>
<int:channel id="routingChannel" >
    <int:queue/>
</int:channel>
<int:header-value-router input-channel="routingChannel" header-name="myHeader">
    <int:poller fixed-delay="0"/>
    <int:mapping value="A" channel="routerAChannel"/>
    <int:mapping value="B" channel="routerBChannel"/>
</int:header-value-router>
<int:channel id="routerAChannel">
    <int:queue/>
</int:channel>
<int:outbound-channel-adapter channel="routerAChannel" ref="serviceImpl1" method="a1">
    <int:poller fixed-delay="0"/>
</int:outbound-channel-adapter>
<int:channel id="routerBChannel">
    <int:queue/>
</int:channel>
<int:outbound-channel-adapter channel="routerBChannel" ref="serviceImpl1" method="a2">
    <int:poller fixed-delay="0"/>
</int:outbound-channel-adapter>
  1. message queue (ActiveMQ) Mac安装ActiveMQ brew install activemq activemq --version activemq start

4.a) listner

<int:channel id="topicChannel" />
<bean name="messageListenerImpl" class="com.example.demo.MainTest.ServiceImpl" />
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL">
        <value>tcp://127.0.0.1:61616?trace=true&amp;keepAlive=true</value>
    </property>
    <property name="useAsyncSend" value="true"/>
</bean>
<int-jms:outbound-channel-adapter channel="topicChannel" destination-name="topic.myTopic" pub-sub-domain="true"/>
<int:channel id="listenerChannel"/>
<int-jms:message-driven-channel-adapter id="messageDrivenAdapter" channel="listenerChannel"
                                        destination-name="topic.myTopic" pub-sub-domain="true"/>

4.b) subscriber

<bean name="messageListenerImpl1" class="com.example.demo.MainTest.ServiceImpl" />
<int:service-activator input-channel="listenerChannel" ref="messageListenerImpl" method="processMessage" />
<int:publish-subscribe-channel id="pubsubChannel" />
<int:outbound-channel-adapter channel="pubsubChannel" ref="messageListenerImpl1" method="a1"/>
<int:outbound-channel-adapter channel="pubsubChannel" ref="messageListenerImpl1" method="a2"/>

ref:bitbucket.org/lvfengjiao/… ref:gitee.com/doubletreel…