Apache MINA和Spring MVC集成
1.创建项目
创建一个Spring MVC的WEB应用项目,导入Spring MVC相关的jar包。
另外还需要导入支持Spring面向切面编程的几个包,如下:
aopalliance.jar
aspectjweaver-1.6.12.jar
ognl-3.0.6.jar
两个日志相关的包,如下:
slf4j-api-1.6.6.jar
slf4j-jdk14-1.6.6.jar
另外导入MINA的包,如下:
mina-core-2.0.7.jar
mina-integration-beans-2.0.7.jar
2.配置
1)配置l
其实就是Spring MVC的相关配置。
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="2.5"
xmlns="java.sun/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="java.sun/xml/ns/javaee
java.sun/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!--Spring MVC的配置-->
<servlet>
<description>Spring MVC的配置</description>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l,l</param-v alue>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--Spring MVC的映射-->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>菜鸟教程springmvc
</welcome-file-list>
</web-app>
2)配置l
也是很普通宾配置项。
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package=""/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix"value="/"/>
<property name="suffix"value=".jsp"/>
</bean>
</beans>
3)配置l
这里只保留了MINA需要的几个bean的配置。
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:p="/schema/p"
xmlns:tx="/schema/tx"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/aop
/schema/aop/spring-aop-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package=""/>
<!--支持aop注解-->
<aop:aspectj-autoproxy/>
<bean class="org.springframework.fig.CustomEditorConfigurer"> <property name="customEditors">
<map>
<entry key="java.SocketAddress">
<bean
class="org.apache.mina.integration.beans.InetSocketAddressEditor"/>
</entry>
</map>
</property>
</bean>
<!--The IoHandler implementation-->
<bean id="minaHandler"class="frame.mina.MinaProtocolHandler"/>
<!--the IoFilters-->
<bean id="executorFilter"class="org.apache.utor.ExecutorFilter"/> <bean id="mdcInjectionFilter"
class="org.apache.mina.filter.logging.MdcInjectionFilter">
<constructor-arg value="remoteAddress"/>
</bean>
<bean id="codecFilter"class="org.apache.dec.ProtocolCodecFilter"> <constructor-arg>
<bean class="org.apache.line.TextLineCodecFactory"/> </constructor-arg>
</bean>
<bean id="loggingFilter"class="org.apache.mina.filter.logging.LoggingFilter"/>
<!--The non-SSL filter chain.-->
<bean id="filterChainBuilder"
class="org.filterchain.DefaultIoFilterChainBuilder">
<property name="filters">
<map>
<entry key="executor"value-ref="executorFilter"/>
<entry key="mdcInjectionFilter"value-ref="mdcInjectionFilter"/>
<entry key="codecFilter"value-ref="codecFilter"/>
<entry key="loggingFilter"value-ref="loggingFilter"/>
</map>
</property>
</bean>
<!--The IoAcceptor which binds to port1235server side-->
<bean id="minaAcceptor"
class="org.ansport.socket.nio.NioSocketAcceptor"
init-method="bind"destroy-method="unbind">
<property name="defaultLocalAddress"value=":1235"/>
<property name="handler"ref="minaHandler"/>
<property name="reuseAddress"value="true"/>
<property name="filterChainBuilder"ref="filterChainBuilder"/> </bean>
</beans>
上面标蓝底的那段,是配置的自已开发的MINA处理客户端请求的服务器端程序。下面标蓝底的配置指明,本服务器端程序起动后的服务器端的端口是1235。
3.程序实现
1)服务器端程序
MinaProtocolHandler.java
frame.mina;
import org.service.IoHandlerAdapter;
import org.session.IdleStatus;
import org.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MinaProtocolHandler extends IoHandlerAdapter{
private final Logger Logger(getClass());
//private final Set<IoSession>sessions=Collections.synchronizedSet(new HashSet<IoSession>());
public void messageReceived(IoSession session,Object message){
String msg=(String)message;
System.out.println("Server Received:"+msg);
session.write("Server Send:"+msg);
//sessions.add(session);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论