目錄查看代碼:
兩種實現方式(註解註冊、註解發現)(配置xml文件註冊、 配置xml文件發現)
其他的都和之前的MVC項目結構一致,唯一不同的是serviceImpl實現類:
實現類的java代碼部分(發佈、service.impl實現類)
dubbo-provider.properties配置文件:
applicationContext-mybatis.xml配置文件
EmpController層(註解就用Reference的註解)
項目要求:
兩種實現方式(註解註冊、註解發現)(配置xml文件註冊、 配置xml文件發現)
我們使用
消費者(一):
發佈者項目結構:
發佈者的項目依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt.emp</groupId>
<artifactId>test-emp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.bjsxt.emp-rpc</groupId>
<artifactId>test-emp-rpc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<!-- mysql依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybatis依賴 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis-spring依賴 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!-- pagehelper依賴 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.3</version>
</dependency>
<!-- druid依賴 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- dubbo全部依賴 -->
<!-- 添加dubbo的依賴 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
<!-- 先排除zookeeper -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.1.0</version>
</dependency>
<!-- 添加zookeeper3.4.x版本 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
</dependencies>
</project>
其他的都和之前的MVC項目結構一致,唯一不同的是serviceImpl實現類:
application-mapper.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- 實例化datasource數據庫連接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://192.168.25.10:3306/cas"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 實例化SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 輸入數據庫連接池 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 掃描Mapper接口,產生代理對象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjsxt.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
application-provider.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 發佈遠程服務 -->
<!-- 1 給發佈的遠程服務器名字 -->
<dubbo:application name="emp-provider"></dubbo:application>
<!-- 2 指定服務的註冊中心 -->
<dubbo:registry protocol="zookeeper"
address="192.168.25.101:2181,192.168.25.101:2182,192.168.25.101:2183"
client="curator"/>
<!-- 3 指定遠程服務的訪問協議和端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--
4 聲明需要暴露的服務對應的接口
interface 需要暴露的服務對應的接口的完全限定名
ref 引用實現類的對象
-->
<dubbo:service interface="com.bjsxt.service.EmpService" ref="empServiceImpl" />
</beans>
application-service.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 掃描業務對象 -->
<context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
<!-- 實例化事務管理器對象 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 聲明事務切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="load*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 進行aop的織入 -->
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
實現類的java代碼部分(發佈、service.impl實現類)
Service實現類:
package com.bjsxt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import com.alibaba.dubbo.config.annotation.Service;
import com.bjsxt.mapper.EmpMapper;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List<Emp> loadEmpListService(Emp emp) {
// TODO Auto-generated method stub
return empMapper.loadEmpListMapper(emp);
}
@Override
public void deleteEmpService(Integer id) {
// TODO Auto-generated method stub
empMapper.deleteEmpMapper(id);
}
}
發佈代碼:
package com.bjsxt.app;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RpcApp {
public static void main(String[] args) {
/****
* 加載spring容器暴露服務
*/
ClassPathXmlApplicationContext
ac=new ClassPathXmlApplicationContext("application-mapper.xml",
"application-service.xml","application-provider.xml");
ac.start();
System.out.println("========暴露服務========");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這樣就可以把代碼發佈到dubbo集羣中
消費者:
pom依賴
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bjsxt.emp</groupId>
<artifactId>test-emp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.bjsxt.emp.manager</groupId>
<artifactId>test-emp-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- =============================== -->
<dependencies>
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- 添加jackson依賴 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!--============== dubbo全部依賴================ -->
<!-- 添加dubbo的依賴 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
<!-- 先排除zookeeper -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.1.0</version>
</dependency>
<!-- 添加zookeeper3.4.x版本 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
</dependencies>
<!-- 聲明需要的插件 -->
<build>
<plugins>
<!-- 添加tomcat插件 -->
<plugin>
<artifactId>tomcat7-maven-plugin</artifactId>
<groupId>org.apache.tomcat.maven</groupId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
resources配置文件:
application-consumer.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 1 給消費端服務起名字 -->
<dubbo:application name="emp-consumer"></dubbo:application>
<!-- 2 指定服務的註冊中心 -->
<dubbo:registry protocol="zookeeper"
address="192.168.25.101:2181,192.168.25.101:2182,192.168.25.101:2183"
client="curator"/>
<!-- 3 獲得遠程服務代理對象,代碼對象在spring容器保存
interface 指定代理對象實現的接口
id 用來引用代理對象
-->
<!-- <dubbo:reference interface="com.bjsxt.service.EmpService"
id="empServiceProxy"></dubbo:reference> -->
<!-- <dubbo:annotation package="com.bjsxt.controller"/> -->
</beans>
springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 掃描controller對象 -->
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!-- 配置註解驅動 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置靜態資源映射 -->
<!-- <mvc:resources location="/xxx/" mapping="/xxx/**"></mvc:resources> -->
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上傳解析器 -->
<dubbo:annotation package="com.bjsxt.controller"/>
<!-- 註冊攔截器 -->
</beans>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>emp-manager</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加載spring容器(spring的xml文件) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-*.xml</param-value>
</context-param>
<!-- 註冊監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器,servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置過濾器,處理post請求的亂碼 -->
<filter>
<filter-name>charset</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Controller層文件
package com.bjsxt.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;
@Controller
public class EmpController {
/*@Autowired(如果配置文件用第一種就用該註解)
private EmpService empService;
*/@Reference
private EmpService empService;
/***
* 處理員工信息的查詢請求
*/
@RequestMapping("/loadEmpList")
public String loadEmpList(Emp emp,Model model) {
List<Emp> result = empService.loadEmpListService(emp);
model.addAttribute("result", result);
return "list";
}
/***
* 處理員工信息刪除請求
*/
@RequestMapping("/deleteEmp")
public String deleteEmp(Integer id) {
empService.deleteEmpService(id);
return "redirect:/loadEmpList";
}
}
發佈者(二)使用註解的方式發佈service實現類
只有在service實現類的時候會有差距。讓我們先看一下配置文件
dubbo-provider.properties配置文件:
#給發佈的遠程服務起名字
dubbo.application.name=dubbo-provider
#指定註冊中心訪問地址
dubbo.registry.address=zookeeper://192.168.220.10:2181
#指定暴露的服務的協議
dubbo.protocol.name=dubbo
#指定暴露的服務的端口
dubbo.protocol.port=20880
applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/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">
<!--【A】連接數據庫獲得數據源-->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--【B】獲得session工廠-->
<bean id="factroy" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
</bean>
<!--【C】掃描mapper文件-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factroy"></property>
<property name="basePackage" value="com.bjsxt.mapper"></property>
</bean>
</beans>
Service實現類
package com.bjsxt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.config.annotation.Service;
import com.bjsxt.mapper.EmpMapper;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;
/**
* 僱員業務層實現類
* @author Administrator
*
*/
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
EmpMapper empMapper;
/**
* 查詢指定條件的僱員
*/
@Override
public List<Emp> select4Condition(Emp emp) {
return empMapper.select4Condition(emp);
}
/**
* 刪除id爲x的員工
*/
@Override
public int delete(Integer id) {
return empMapper.delete(id);
}
}
發佈代碼:
package com.bjsxt.provider;
import java.io.IOException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
public class EmpProvider {
public static void main(String[] args) {
// 加載spring容器
AnnotationConfigApplicationContext
context = new AnnotationConfigApplicationContext(Configer.class);
// 啓動spring容器
context.start();
System.out.println("==============完成服務的發佈=============");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Configuration // 實例化靜態內部類所在的類
@EnableDubbo(scanBasePackages = "com.bjsxt.service.impl")
@PropertySource(value = "classpath:/dubbo-provider.properties")
@ImportResource(value= {"applicationContext-mybatis.xml"})
//指定需要掃描Spring組件所在的包
@ComponentScan(value = {"com.bjsxt.controller"})
static class Configer {
}
}
消費者(二)
dubbo-consumer.properties配置文件
#給發佈的遠程服務起名字
dubbo.application.name=emp-consumer
#指定註冊中心訪問地址
dubbo.registry.address=zookeeper://192.168.25.101:2181
springmvc配置文件
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 掃描controller對象 -->
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!-- 配置註解驅動 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置靜態資源映射 -->
<!-- <mvc:resources location="/xxx/" mapping="/xxx/**"></mvc:resources> -->
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上傳解析器 -->
<!-- <dubbo:annotation package="com.bjsxt.controller"/> -->
<!-- 註冊攔截器 -->
</beans>
controller層文件
第一個是用來加載靜態代碼塊配置dubbo的,第二個就是controller層的代碼
ConsumerConfig配置文件
package com.bjsxt.controller;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@Configuration //將靜態內部類實例化
//加載dubbo-consumer.properties
@PropertySource("classpath:/dubbo-consumer.properties")
//指定暴露的服務接口對應的包
@EnableDubbo(scanBasePackages = "com.bjsxt.service")
//指定需要掃描Spring組件所在的包
//@ComponentScan(value = {"com.bjsxt.controller"})
public class ConsumerConfig {
public ConsumerConfig() {
System.out.println("=========================");
}
}
EmpController層(註解就用Reference的註解)
package com.bjsxt.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;
@Controller
public class EmpController {
/*@Autowired
private EmpService empService;
*/@Reference
private EmpService empService;
/***
* 處理員工信息的查詢請求
*/
@RequestMapping("/loadEmpList")
public String loadEmpList(Emp emp,Model model) {
List<Emp> result = empService.loadEmpListService(emp);
model.addAttribute("result", result);
return "list";
}
/***
* 處理員工信息刪除請求
*/
@RequestMapping("/deleteEmp")
public String deleteEmp(Integer id) {
empService.deleteEmpService(id);
return "redirect:/loadEmpList";
}
}
註解實現文件的發現,與之前註解啓動發現服務不同的是需要main方法來加載容器,我們這裏把類放在了controller層中,這樣spring容器啓動會掃描controller層,久會掃描到該類的註解,創建對象讀取數據等等一系列的配置