我有一个接口(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/