gpt4 book ai didi

java - 如何根据属性文件中的设置强制服务使用特定存储库?

转载 作者:行者123 更新时间:2023-12-02 02:58:25 25 4
gpt4 key购买 nike

我将虚拟机选项设置为-Dmyapp.conf=config/my.properties。我需要使用此文件内容来选择要使用的存储库。我使用SpringBoot。

有服务,现在它给出无法 Autowiring 。存在多个“MyRepository”类型的 bean:

@Service
public class MyServiceImpl implements MyService {

@Autowired
private MyRepository repository;
...}

存储库:

public interface MyRepository  {...}

@Repository
public class MyRepositoryImpl01 implements MyRepository {...}

@Repository
public class MyRepositoryImpl02 implements MyRepository {...}

最佳答案

您可以使用Spring profiles为此目的。

@Profile("profile1")
@Repository
public class MyRepositoryImpl01 implements MyRepository {...}

@Profile("profile2")
@Repository
public class MyRepositoryImpl02 implements MyRepository {...}

然后在属性文件中您需要:spring.profiles.active=profile1spring.profiles.active=profile2

关于java - 如何根据属性文件中的设置强制服务使用特定存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com