androidpn server 通过Spring与restful webservice整合

22 篇文章 0 订阅
17 篇文章 0 订阅


前面说过androidpn server的启动日志分析,有个小错误:

就是在  XmppServer 的start()方法里面有一个加载spring-config.xml的步骤。


在这里加载的spring context 与在web.xml中通过springContextListeners加载的上下文环境是相互独立的。


之前的文章也说过,restful webservice通过spring进行加载。


本文说明如何修改androidpn,让androidpn 与 restful webservice 相互可见。


一、禁止XmppServer加载spring-config.xml文件

1、注释掉 spring-config.xml加载

 
            locateServer();
            serverName = Config.getString("xmpp.domain", "127.0.0.1").toLowerCase();
//          context = new ClassPathXmlApplicationContext("spring-config.xml");
            log.info("Spring Configuration loaded.");
 

2、注释掉getBean()

 
//    public Object getBean(String beanName) {
//        return context.getBean(beanName);
//    }

3、注释掉成员变量

 
//    private ApplicationContext context;
 

二、修改 ServiceLocator

1、 ServiceLocator 实现 ApplicationContextAware 接口

2、增加 ApplicationContext context变量

3、增加 ServiceLocator serverLocator 变量

 
public class ServiceLocator implements ApplicationContextAware {

	private static ApplicationContext context = null;

	private static ServiceLocator servlocator = null;
 

4、增加 ApplicationContext 的Setter/Getter

 
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		context = arg0;
	}

	public ApplicationContext getApplicationContext() {
		return ServiceLocator.context;
	}
 

5、增加getInstance()方法

6、增加getBean() 方法

 
	public static ServiceLocator getInstance() {
		if (servlocator == null)
			servlocator = (ServiceLocator) context.getBean("serviceLocator");
		return servlocator;
	}


	public Object getBean(String name) {
		return context.getBean(name);
	}
 

7、将原来的方法中对XmppServer.getInstance()的引用替换为 context

 
	public static Object getService(String name) {
		return context.getBean(name);
	}


	public static UserService getUserService() {
		return (UserService) context.getBean(USER_SERVICE);
	}
 

三、配置文件修改

1、将ServiceManager对XmppServer.getInstance()的引用改为对ServiceLocator.getInstance()

 
    public static Object getService(String name) {
        return ServiceLocator.getInstance().getBean(name);
    }


    public static UserService getUserService() {
        return (UserService) ServiceLocator.getInstance().getBean(USER_SERVICE);
    }
 

2、配置文件的修改

在spring-config.xml中加入


<bean id="serviceLocator" class="org.androidpn.server.service.ServiceLocator" scope="singleton" /> 


3、修改spring-config.xml 为 mina-androidpn-config-context.xml 放到spring的扩展文件路径下   WEB-INF\classes\conf\extension

因为在spring的application-context.xml中有这样一句引用:

    <import resource="classpath:conf/extension/*-context.xml"/>



四、启动

五、结果

所有的bean都在  ServiceLocator 中可见。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值