androidpn server 通过Spring与restful webservice整合

来源:互联网 发布:淘宝怎么发安能快递 编辑:程序博客网 时间:2024/06/02 02:02


前面说过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

 @Overridepublic 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 0
原创粉丝点击