我将虚拟机选项
设置为-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=profile1
或 spring.profiles.active=profile2
。
关于java - 如何根据属性文件中的设置强制服务使用特定存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736876/