• 在Spring中DAO与Service关于依赖注入写法探讨


    在spring中配置事务的时候,可能会遇到两种情况,一种是针对DAO层的事务拦截,还有一种是针对Service的拦截,看论坛里其实比较多的人倾向 与在service的拦截(DAO只是进行简单的CRUD操作,而Service根据业务规则来选取必要的DAO进行操作),现在要探讨的不是在哪个层面 的优势在哪里,而是只是简单基于Service的探讨配置写法哪种:
    方法一:
    Xml代码 复制代码
    1. <bean id = "dao1" class = "com.test.Dao1" />  
    2. <bean id = "dao2" class = "com.test.Dao2" />  
    3. <bean id = "service1" class = "com.test.Service1">  
    4.    <property name="dao1" ref="dao1" />  
    5.    <property name="dao2" ref="dao2" />  
    6. </bean>  
    7. <bean id = "dao3" class = "com.test.Dao3" />  
    8. <bean id = "dao4" class = "com.test.Dao4" />  
    9. <bean id = "service3" class = "com.test.Service3">  
    10.    <property name="dao3" ref="dao3" />  
    11.    <property name="dao4" ref="dao4" />  
    12. </bean>  
    13. <bean id = "bizService" class = "com.test.BizService">  
    14.    <property name="dao1" ref="dao1" />  
    15.    <property name="dao2" ref="dao2" />  
    16.    <property name="dao3" ref="dao3" />  
    17.    <property name="dao4" ref="dao4" />  
    18. </bean>  
    <bean id = "dao1" class = "com.test.Dao1" /> <bean id = "dao2" class = "com.test.Dao2" /> <bean id = "service1" class = "com.test.Service1"> <property name="dao1" ref="dao1" /> <property name="dao2" ref="dao2" /> </bean> <bean id = "dao3" class = "com.test.Dao3" /> <bean id = "dao4" class = "com.test.Dao4" /> <bean id = "service3" class = "com.test.Service3"> <property name="dao3" ref="dao3" /> <property name="dao4" ref="dao4" /> </bean> <bean id = "bizService" class = "com.test.BizService"> <property name="dao1" ref="dao1" /> <property name="dao2" ref="dao2" /> <property name="dao3" ref="dao3" /> <property name="dao4" ref="dao4" /> </bean>
    方法二:
    Xml代码 复制代码
    1. <bean id = "dao1" class = "com.test.Dao1" />  
    2. <bean id = "dao2" class = "com.test.Dao2" />  
    3. <bean id = "service1" class = "com.test.Service1">  
    4.    <property name="dao1" ref="dao1" />  
    5.    <property name="dao2" ref="dao2" />  
    6. </bean>  
    7. <bean id = "dao3" class = "com.test.Dao3" />  
    8. <bean id = "dao4" class = "com.test.Dao4" />  
    9. <bean id = "service3" class = "com.test.Service3">  
    10.    <property name="dao3" ref="dao3" />  
    11.    <property name="dao4" ref="dao4" />  
    12. </bean>  
    13. <bean id = "bizService" class = "com.test.BizService">  
    14.    <property name="service1" ref="service1" />  
    15.    <property name="service3" ref="service3" />  
    16. </bean>  
    <bean id = "dao1" class = "com.test.Dao1" /> <bean id = "dao2" class = "com.test.Dao2" /> <bean id = "service1" class = "com.test.Service1"> <property name="dao1" ref="dao1" /> <property name="dao2" ref="dao2" /> </bean> <bean id = "dao3" class = "com.test.Dao3" /> <bean id = "dao4" class = "com.test.Dao4" /> <bean id = "service3" class = "com.test.Service3"> <property name="dao3" ref="dao3" /> <property name="dao4" ref="dao4" /> </bean> <bean id = "bizService" class = "com.test.BizService"> <property name="service1" ref="service1" /> <property name="service3" ref="service3" /> </bean>

    大家觉得在写BizService的时候注入是用service的注入好呢还是用dao的注入好呢!一起谈谈你们的看法。
    个人认为方法一调理比较清晰但较为繁琐,因为如果service1中有了业务逻辑可能到了bizService还要再写一遍,方法二比较简单但到了BizService的时候可能对DAO的调用概念有所模糊化了。对于数据库事务拦截问题,不知道这两种方法是否会有所区别?

    第二种
    service中注入dao
    service中注入service的话 有时候会引起循环依赖的
    再说 从设计上说 service中注入service是不合理的
    不能很好体现分层

    我是用第1种方法,但配置明显复杂了,用byName或annotation自动注入的写法,如果已经有service中的方法包含了复杂的业务逻辑写法,而其它service也会用到的话,抽取出来,变成公用的方法,反正尽量减少重复的代码
  • 相关阅读:
    一种不求交点确定直线与三角形是否相交的方法
    碰撞边界锯齿的平滑方法
    demo的凹凸贴图效果
    MySQL进阶篇触发器
    MySQL进阶篇索引
    Maven的POM文件详解
    Swagger
    MySQL进阶篇存储过程
    SpringBoot基础篇
    MySQL基础篇多表操作
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400837.html
Copyright © 2020-2023  润新知