gpt4 book ai didi

java - Spring:根据配置文件选择@Service

转载 作者:IT老高 更新时间:2023-10-28 13:53:55 27 4
gpt4 key购买 nike

我有一个接口(interface)定义如下:

public interface MyService {
}

还有两个实现它的类:

@Service
@Profile("dev")
public class DevImplementation implements MyService {
}

@Service
@Profile("prod")
public class ProdImplementation implements MyService {
}

还有另一个服务试图使用它:

@Service
public MyClass {
@Autowired
MyService myservice;
}

问题是我在运行代码时收到 NoSuchBeanException。它使用

运行

mvn spring-boot:run -P dev

我做错了什么?

最佳答案

使用 -P 可以启用 Maven 配置文件。但是 Maven 配置文件独立于 Spring 配置文件。只要您没有配置 Maven 配置文件来设置适当的 Spring 属性,您就必须以这种方式启用 Spring 配置文件:

mvn spring-boot:run -Dspring.profiles.active=dev

关于java - Spring:根据配置文件选择@Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41891728/

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