企业申请入驻功能
本质就是往ss_company插入一条记录
- (1)分析
目前,后台系统中已经完成添加企业功能
现在,需要开发前端系统,完成企业入驻申请功能
与后台功能一样。都是往企业表保存数据 - (2)如何解决前端系统企业入驻功能实现?
》 方案1: 前端系统自己实现企业入驻,此时需要编写domain、dao、service、controller等。
》 方案2:通过公共的dubbo服务完成统一的CompanyServiceImpl
共用接口工程
- (1)实现步骤
》 创建项目:export_company_interface
》 复制服务的接口
》 添加依赖
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>export_domain</artifactId>
<groupId>com.wzx</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
-
如果没有依赖domain模块,则接口报错
实现类-dubbo提供者
- (1)分析
Controller 使用dubbo 调用 Service,后者是provider,前者是cosumer - (2)实现步骤
》1 创建项目:export_company_service
》2 复制服务接口实现类
》3 添加依赖:服务接口工程、dao工程、dubbo相关依赖
pom.xml
<!-- 依赖parent里面的spring等-->
<parent>
<groupId>com.wzx</groupId>
<artifactId>export_parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.wzx</groupId>
<artifactId>export_company_service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>export_company_service</name>
<!--
provider就是将Service实现类使用dubbo发布出来,给别的工程调用
建议使用web模板创建,以后运行在tomcat
-->
<dependencies>
<!-- 依赖 dao-->
<dependency>
<groupId>com.wzx</groupId>
<artifactId>export_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 依赖 service接口-->
<dependency>
<groupId>com.wzx</groupId>
<artifactId>export_company_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 依赖dubbo-->
<!--dubbo相关-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
》4 配置web.xml
web.xml
<!-- main-->
<!-- 监听器监听其他的spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
》5 配置dubbo:applicationContext-dubbo.xml
配置dubbo
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--1.dubbo的服务名称-->
<dubbo:application name="export_company_service"/>
<!--2.Zookeeper注册配置 zookeeper在实际开发中一般单独在一台机器上
address配置该机器的ip,在开发时,写localhost
-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--3.dubbo协议-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--4.扫描dubbo的@Service注解所在包-->
<dubbo:annotation package="com.wzx.service"/>
</beans>
》6 配置事务:applicationContext-tx.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--包扫描:开启注解支持-->
<context:component-scan base-package="com.wzx.service"/>
<!--配置事务-->
<!--1.事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--2.事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="save*" read-only="false" propagation="REQUIRED"/>
<tx:method name="update*" read-only="false" propagation="REQUIRED"/>
<tx:method name="delete*" read-only="false" propagation="REQUIRED"/>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--3.aop-->
<aop:config>
<aop:pointcut id="pt" expression="execution( * com.wzx.service.*.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>
实现类-dubbo提供者测试
- (1)测试
》7 通过main函数启动服务
cxt.start();
/**
* 企业管理服务提供者
*/
public class CompanyProvider {
public static void main(String[] args) throws IOException {
//1.加载配置文件
ClassPathXmlApplicationContext
cxt = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-*.xml");
//2.启动
cxt.start();
//3.阻塞
System.in.read();
}
}