Mina学习之与Spring整合
本章中演示在Spring中整合Mina,为了整合到Spring,需要做以下几个步骤:
1. 设置IoHandler
<bean id="trapHandler" class="com.ashishpaliwal.udp.mina.server.ServerHandler">
2. 创建Filters并加入到Chain中
<bean id="snmpCodecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter"> <constructor-arg> <bean class="com.ashishpaliwal.udp.mina.snmp.SNMPCodecFactory" /> </constructor-arg> </bean> <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" /> <!-- The filter chain. --> <bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder"> <property name="filters"> <map> <entry key="loggingFilter" value-ref="loggingFilter"/> <entry key="codecFilter" value-ref="snmpCodecFilter"/> </map> </property> </bean>
3. 创建IoAcceptor
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.net.SocketAddress"> <bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" /> </entry> </map> </property> </bean> <!-- The IoAcceptor which binds to port 161 --> <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioDatagramAcceptor" init-method="bind" destroy-method="unbind"> <property name="defaultLocalAddress" value=":161" /> <property name="handler" ref="trapHandler" /> <property name="filterChainBuilder" ref="filterChainBuilder" /> </bean>
4. 在spring启动时加载该xml文件
public void initializeViaSpring() throws Exception { new ClassPathXmlApplicationContext("trapReceiverContext.xml"); }