每日一得

不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速开发
最近关心的内容:SSH,seam,flex,敏捷,TDD
本站的官方站点是:颠覆软件

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  220 随笔 :: 9 文章 :: 421 评论 :: 0 Trackbacks

 

key word : spring,service

在基于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");
    }

    
    }



ApplicationContext的初始化:

public class ConfigLoadListener implements ServletContextListener {

    
public void contextInitialized(ServletContextEvent contextEvent) {  
        
try {
            WebApplicationContext context =WebApplicationContextUtils.getRequiredWebApplicationContext(contextEvent.getServletContext());
            AppContext.setApplicationContext(context);

            
//读配置
            try {
                ServletContext context2
=contextEvent.getServletContext();
                String path
=context2.getInitParameter("setting.properties");
                InputStream in 
=context2.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) {
    }

}


 感觉有点麻烦,有更简便的办法了么?

update(2007-5-15):
这篇文章大家可以参考一下,和本文有类似之处:
 Struts调用Spring服务类的三种方法

引用:

3.朋友帮忙型  朋友多了好办事,凡事都自己动手总有一天会活活累死,所有Action为自己需要的Service提供setter,并且在Spring中注入,好累啊。小学生都在减负了,我们也来为Action减减负吧--提供一个专门  
查找Serivice的ServiceLocator,负责获取所有的Service,该类为每个Service提供专门的获得方法,如:  


 

posted on 2007-05-14 09:05 Alex 阅读(13331) 评论(8)  编辑  收藏 所属分类: javaSpring

评论

# re: Spring中的service之间如何调用 2007-05-14 09:23 刘甘泉
有啊,在spring里配置上b注入a即可,不过可能失去spring的事务性控制。
另,感觉你的设计可能有问题,所以才会有serviceA掉用serviceB的问题。
相关的业务都是放到一个service里面的,如serviceA.executeA()里面调用了daoA.insert和daoB.update 而serviceB.executeB()调用了daoB.update,看起来可以换成是serviceA调用了serviceB,其实不对的,一个事务应该保持事务的一致性,即一个事务只应该做一个事务的事。
而你的那种配置方法是在serviceA中调用serviceB,serviceB本身就是由spring 控制的事务,当你在serviceA中调用过serviceB.后,spring会自动提交serviceB的事务,并不是在serviceA完成的时候提交。

如有不对请指教  回复  更多评论
  

# re: Spring中的service之间如何调用 2007-05-14 09:29 dennis
用了spring,还在action中使用getBean?奇怪了  回复  更多评论
  

# re: Spring中的service之间如何调用 2007-05-14 09:46 dennis
struts和spring的整合不应该在action中使用getBean的方式,这完全背离了使用spring的初衷,造成了对spring的依赖,更好的方式请看江南大侠http://www.blogjava.net/calvin/archive/2006/11/17/81711.html

service的互相调用,原来怎么配,现在也怎么配,只要在spring配置文件中设置下依赖关系,使用的service有个setter方法就可以了。看看这篇文章http://www.javaeye.com/topic/35907  回复  更多评论
  

# re: Spring中的service之间如何调用 2007-05-14 10:05 刘甘泉
不错 ,嵌套事务,PROPAGATION_NESTED,transactiondefinition就可以了  回复  更多评论
  

# re: Spring中的service之间如何调用 2007-05-15 11:17 Alex
to dennis
我个人不太喜欢在action中注入service,感觉罗嗦了
service中注入dao就可以了,到处在注入感觉很不爽,个人感觉.  回复  更多评论
  

# re: Spring中的service之间如何调用 2007-05-15 14:41 刘甘泉
@Alex
证明你的分层思想还不到位~~,dao只是对于数据库的操作,并不涉及业务流程,而一个业务流程可能经过了n个dao的操作,如果注入到action中的话,那维护起来不是很麻烦,而且事务管理的好处也不在了~~~  回复  更多评论
  

# re: Spring中的service之间如何调用 2014-03-09 20:53 xxli
对Spring事务传播行为最常见的一个误解是:当服务接口方法发生嵌套调用时,被调用的服务方法只能声明为 PROPAGATION_NESTED。这种观点犯了望文生义的错误,误认为PROPAGATION_NESTED是专为方法嵌套准备的。这种误解遗害不 浅,执有这种误解的开发者错误地认为:应尽量不让Service类的业务方法发生相互的调用,Service类只能调用DAO层的DAO类,以避免产生嵌 套事务。  回复  更多评论
  

# re: Spring中的service之间如何调用 2014-03-09 20:54 xxli
具体查看文章http://www.cnblogs.com/aurawing/articles/1887030.html  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: