开始
是在之前的项目上
https://blog.csdn.net/ko0491/article/details/85168055
只更改Main类
之前的
现在的
public class Main3 {
public static void main(String[] args) throws IOException {
// com.alibaba.dubbo.container.Main.main(args);
//
// ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
// new String[]{ "META-INF/spring/service-provider.xml" });
/**
* 使用spring文件
*/
/*
* ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{
* "META-INF/spring/spring.xml" }); context.start();
*/
com.alibaba.dubbo.container.Main.main(args);
System.out.println("provider启动了");
}
}
是不是很简单,有点小意外
com.alibaba.dubbo.container.Main.main方法介绍
public static final String CONTAINER_KEY = "dubbo.container";
args传进来是空
config最终获取到的是spring
args = spring
再向下
将容器添加到list集合中,只有一个
是spring容器
这个将会跳过,不匹配,关闭服务时
循环窗口集合,上面的只有一个
com.alibaba.dubbo.container.spring.SpringContainer
在start()方法
com.alibaba.dubbo.container.spring.SpringContainer start()方法
介绍下几个常量
public static final String SPRING_CONFIG = "dubbo.spring.config";
public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";
private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class);
static ClassPathXmlApplicationContext context;
获取配置,默认没有
最终就是这个路径了
public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";
写死了下面就是创建spring ApplicationContext过程与启动
我们的项目结构
配置文件刚好在META-INF/spring下,自动加载这个目录下的所有spring配置文件
spring.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 当我们需要使用 注解时 开启注解 -->
<context:annotation-config />
<!--配置扫描包 -->
<context:component-scan base-package="com.ghgcn.dubbo.service" />
<!-- 这里不用了,会自动加载这个目录下的所有spring配置文件 -->
<!-- <import resource="service-provider.xml"/> -->
</beans>
service-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-provider" />
<!-- 注册中心地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 声明协议与商品 -->
<dubbo:protocol name="dubbo" port="20886" />
<!-- 声明服务的实现类 旧的方式-->
<!-- <bean id="greetingService" class="com.ghgcn.dubbo.service.impl.GreetingServiceImpl"
/> -->
<!-- 声明服务 spring -->
<dubbo:service interface="com.ghg.dubbo.api.GreetingService"
ref="greetingService" protocol="dubbo" validation="false" />
</beans>