Java Spring数据:几种实现
在通常的SpringMVC中,我可以为不同的Spring概要文件创建一个Dao接口以及该接口的几个实现。但如果我想实现Spring数据,有一个问题:如何创建这些实现?添加自定义存储库(如上所述)的想法永远无法解决此问题 例如。 道: DaoExampleImpl:Java Spring数据:几种实现,java,spring-data,Java,Spring Data,在通常的SpringMVC中,我可以为不同的Spring概要文件创建一个Dao接口以及该接口的几个实现。但如果我想实现Spring数据,有一个问题:如何创建这些实现?添加自定义存储库(如上所述)的想法永远无法解决此问题 例如。 道: DaoExampleImpl: @Profile("someProfile") public class DaoExampleImpl implements DaoExampleCustom { @Override public void someM
@Profile("someProfile")
public class DaoExampleImpl implements DaoExampleCustom {
@Override
public void someMethod(User user) {
}
}
虽然DaoExample和DaoExampleImpl属于不同的概要文件,但此代码将无法启动。此外,如果我想为不同的配置文件创建几个DaoExampleImpl,该怎么办?我应该将它们命名为DaoExampleImpl1、DaoExampleImp2等,但这违反了Spring数据的命名约定。这是否意味着如果我需要为几个Spring概要文件创建一个dao层,我应该完全避免使用Spring数据?如果我理解正确,您需要多个
DaoExampleImpl
类
@Profile("someProfile")
public class DaoExampleImpl implements DaoExampleCustom
@Profile("someProfile2")
public class DaoExampleImpl implements DaoExampleCustom
你们不能把它们放在一个单独的包中,并根据所需的配置文件调整包扫描以包括必要的包吗。因此,Spring数据命名约定是安全的,您可以使用不同的实现。只是一个想法。可能有一些黑客可以让它工作。@StanslavL提出的方案应该有效 另一种方法是将实现注册为Bean,我认为这应该优先于Spring数据找到的类,它必须注册自己 描述了一个类似的问题和一个使用不同“repositoryImplementationPostfix”的解决方法,您可以将其放在不同的配置文件上
但所有这些方法对我都不太有吸引力。相反,我将提供一个实现类,然后将实际工作委托给另一个bean,您可以使用概要文件切换该bean。这样,您就不必依赖于Spring数据的特殊处理方式 我不完全理解这些要求。你能不能只添加
公共接口DaoExample扩展了JpaRepository,DaoExampleCustom
?对不起,我忘了在代码示例中添加这个。我已经更正了文本。这可能有用,但是。。。最后,它看起来比在没有Spring数据的情况下创建dao层更困难。所以,我认为使用Spring数据的整个想法在只有“实现”的情况下是有意义的。否则就应该避免
@Profile("someProfile")
public class DaoExampleImpl implements DaoExampleCustom {
@Override
public void someMethod(User user) {
}
}
@Profile("someProfile")
public class DaoExampleImpl implements DaoExampleCustom
@Profile("someProfile2")
public class DaoExampleImpl implements DaoExampleCustom