方法一:
Xml代码
- <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代码
- <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也会用到的话,抽取出来,变成公用的方法,反正尽量减少重复的代码