如果是传统的话可以用带参数的构造函数,但是 spring 中有什么好方法?
我目前只能用 e.printstacktrace 的黑科技,但是代码总感觉怪怪的。
我目前只能用 e.printstacktrace 的黑科技,但是代码总感觉怪怪的。
1
jatesun Jan 6, 2018
觉得还是从设计角度找找问题吧
|
2
lhx2008 OP @jatesun 主要是有个 BaseService 的需求,不同的 Service 在创建的时候自动指派 dao,所以 BaseService 必须要知道是哪个子 service 才能给他分配
|
6
Charkey Jan 6, 2018 public class BaseServiceImpl {
private BaseRepository<E, K> baseRepository; public BaseServiceImpl(BaseRepository<? super E, ? super K> baseRepository) { this.baseRepository =(BaseRepository<E, K>)baseRepository; } } public class CustOrderServiceImpl { CustOrderRepository custOrderRepository; @Autowired public CustOrderServiceImpl(CustOrderRepository custOrderRepository) { super(custOrderRepository); this.custOrderRepository = custOrderRepository; } } 粗略的代码,大概这样? |
7
GoodRainChen Jan 6, 2018
可以用模板方法模式
|
8
lotmany Jan 6, 2018 via iPhone
看一下通用 mapper 我感觉他推荐的泛型注入的方式就是你想要的。
|
9
grq100296 Jan 12, 2018
泛型应该可以吧,类似 mybaties plus 里提供的 IService
|