ClassPathXmlApplicationContext start 方法

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/AbstractApplicationContext.html#start--  官方文档


http://stackoverflow.com/questions/24145070/use-of-start-method-in-classpathxmlapplicationcontext   其他网站搜索得到的


简单copy过来,只是为了记录一下。正确与否不知道。。

Spring provides a ApplicationListener interface to listen for various ApplicationEvents. One such event is the ContextStartedEvent. You can create a bean of some class that implements ApplicationListener<ContextStartedEvent>.

When you invoke start(), the ApplicationContext will publish such an event and allApplicationListener beans registered for ContextStartedEvent (they get registered automatically if found in the context) will get notified.

If you don't call start(), they will not get notified.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Dubbo 客户端和服务端调用示例: 服务端: 1. 定义接口 ```java public interface HelloService { String sayHello(String name); } ``` 2. 实现接口 ```java public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 3. 配置服务 在 resources 目录下创建 dubbo 目录,并在其中新建一个名为 provider.xml 的文件,配置如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:service interface="com.example.demo.HelloService" ref="helloService" timeout="5000" version="1.0.0"> </dubbo:service> <bean id="helloService" class="com.example.demo.HelloServiceImpl"/> ``` 4. 启动服务 ```java public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo/provider.xml"}); context.start(); System.in.read(); // 按任意键退出 } } ``` 客户端: 1. 配置服务 在 resources 目录下创建 dubbo 目录,并在其中新建一个名为 consumer.xml 的文件,配置如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="helloService" interface="com.example.demo.HelloService" version="1.0.0"/> ``` 2. 调用服务 ```java public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo/consumer.xml"}); context.start(); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.sayHello("Dubbo"); System.out.println(result); } } ``` 使用 ZooKeeper 作为注册中心,服务端和客户端需要先启动 ZooKeeper,然后再启动服务端和客户端。 以上是一个简单的 Dubbo 客户端和服务端调用示例,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值