Mina框架与Spring的整合其实很简单,主要是要弄清楚要注入的属性的名称,进而选择合适的注入方法。
关于Spring的四种注入方法请另一篇文章:spring依赖注入的四种方式
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 业务处理逻辑 -->
<bean id="handler" class="com.mina.handler.StrategyFactoryHandler" />
<!-- 累加数据包解码器:解断丢包、粘包问题 -->
<bean id="codec" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
<constructor-arg>
<bean class="com.mina.codec.MyCodecFcatory">
<constructor-arg index="0">
<bean class="com.mina.codec.MyEncoder"></bean>
</constructor-arg>
<constructor-arg index="1">
<bean class="com.mina.codec.MyDecoder">
</bean>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
<!-- 多线程处理过滤器,为后面的操作开启多线程,一般放在编解码过滤器之后,开始业务逻辑处理 -->
<bean id="executors" class="org.apache.mina.filter.executor.ExecutorFilter"/>
<!-- Mina自带日志过滤器 默认级别为debug -->
<bean id="loggerFilter" class="org.apache.mina.filter.logging.LoggingFilter">
<property name="messageReceivedLogLevel" ref="info"></property>
<property name="exceptionCaughtLogLevel" ref="info"></property>
</bean>
<!-- 枚举类型 依赖注入 需要先通过此类进行类型转换-->
<bean id="info" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="org.apache.mina.filter.logging.LogLevel.INFO" />
</bean>
<bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
<property name="filters">
<map>
<entry key="codec" value-ref="codec"/>
<entry key="logger" value-ref="loggerFilter"/>
<entry key="executors" value-ref="executors"/>
</map>
</property>
</bean>
<!-- 监听端口 -->
<bean id="defaultLocalAddress" class="java.net.InetSocketAddress">
<constructor-arg index="0" value="${tcpPort}"></constructor-arg>
</bean>
<!-- session config 通过工厂方法注入 -->
<bean id="sessionConfig" factory-bean="ioAcceptor" factory-method="getSessionConfig" >
<property name="readerIdleTime" value="10"/>
<property name="minReadBufferSize" value="512"/>
<property name="maxReadBufferSize" value="10240"/>
</bean>
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
init-method="bind" destroy-method="unbind">
<!-- 默认启用的线程个数是CPU 的核数+1, -->
<!--<constructor-arg index="0" value="10"></constructor-arg>-->
<property name="defaultLocalAddress" ref="defaultLocalAddress" />
<property name="handler" ref="handler" />
<property name="filterChainBuilder" ref="filterChainBuilder" />
</bean>
</beans>
Mina框架与Spring整合配置文件