指点成金-最美分享吧

登录

知识大全 Spring中的service之间如何调用

佚名 举报

篇首语:同时赶两只兔,一只也捉不到。本文由小编为大家整理,主要介绍了知识大全 Spring中的service之间如何调用相关的知识,希望对你有一定的参考价值。

Spring中的service之间如何调用  以下文字资料是由(全榜网网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

    在基于struts+spring+hibernate的开发框架下 一般service都是直接通过在Struts的action中getBean( yourServiceName )来获取 那么如果在serviceA中想调用serviceB中的方法该如何呢?    直接new 一个serviceB是不行的 因为里面可能还有依赖注入的dao等其他本来需要容器管理的资源 可以象在action中一样getBean()么?    获得applicationContext就可以了 AppContext public class AppContext     private static ApplicationContext applicationContext;        public static ApplicationContext getApplicationContext()         return applicationContext;        public static void setApplicationContext(            ApplicationContext applicationContext)         AppContext applicationContext = applicationContext;    

  SpringService

public class SpringBeanService     private static SpringBeanService instance;    private ApplicationContext applicationContext;    public static synchronized SpringBeanService getInstance()         if (instance == null)             instance = new SpringBeanService();                return instance;        public ApplicationContext getApplicationContext()         return this applicationContext;        public void setApplicationContext(ApplicationContext applicationContext)         this applicationContext = applicationContext;        public UserService getUserService()        return (UserService)AppContext getApplicationContext() getBean( userService );             public class ConfigLoadListener implements ServletContextListener     public void contextInitialized(ServletContextEvent contextEvent)           try             WebApplicationContext context =WebApplicationContextUtils getRequiredWebApplicationContext(contextEvent getServletContext());            AppContext setApplicationContext(context);            //读配置            try                 ServletContext context =contextEvent getServletContext();                String path=context getInitParameter( setting properties );                InputStream in =context getResourceAsStream(path);                Properties properties = new Properties();                properties load(in);                GlobalConstant setCmdbProperties(properties);                in close();             catch (IOException e)                 e printStackTrace();                     catch (HibernateException e)             System out println( 系统无法初始化 异常退出 );            System out println(e);                   public void contextDestroyed(ServletContextEvent contextEvent)      lishixinzhi/Article/program/Java/ky/201311/28919