呵呵 经过几天的努力终于把这个demo完成了 感觉收获了不收东西,所以想记录下来.
上次我们说到了如何搭建dubbo+zookeeper和如何管理dubbo的服务,这回我们将spring配置进去这样一个完整的框架就搭建好了
1.首先先创建一个项目这是我的项目目录:
2.里面dubbo和zookeeper的配置在上篇文章已经说过了,所以说现在重点看一下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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 开启扫描 --> <context:component-scan base-package="com.baidu.*"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/exam"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 建立sqlsessionfactory的配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage"> <value>com.jlc.vo.*</value> </property> <property name="mapperLocations"> <value>classpath:mybatis/*.xml</value> </property> </bean> <!-- 扫描我dao层接口中的方法 根据 方法名匹配xml文件的sql的id --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage"> <value>com.jlc.dao</value> </property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务处理 --> <aop:config><!--public * com.baidu.biz.impl.*BizImpl.*(..)--> <aop:pointcut expression="execution(* com.baidu.service.impl.*.*(..))" id="trPointcut"/> <aop:advisor advice-ref="trAdvice" pointcut-ref="trPointcut"/> </aop:config> <tx:advice id="trAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="upd*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <!-- 导入dubbo的配置文件 --> <import resource="applicationProvider.xml"/> </beans>
3.如果配置没有问题的话,那么启动main方法就行啦
4.客户端,在创建一个web项目
5.来看下spring配置文件的内容
<context:component-scan base-package="com.baidu"/> <import resource="applicationProvider.xml"/>因为这里spring并不需要连接数据库和管理事物,多以说值需要加载一下dubbo服务即可
6.看下springMVC配置文件的内容
<context:component-scan base-package="com.jlc"/> <mvc:annotation-driven/> <!--<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven>--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"></property> </bean>很简单 配置下是视图简析器就可以了
这样就完成了dubbo+zookeeper+spring 框架的搭建了