簡體   English   中英

Spring 引導 - 使用配置文件的限定符

[英]Spring Boot - Using Qualifiers with Profiles

我有以下使用限定符和配置文件dev !dev Service1將使用基於限定符的FooImpl1並且Service2將使用FooImpl2 當配置文件是dev我希望兩個服務都使用FooImpl3 我怎樣才能做到這一點?

interface Foo {
  public void fooToYou();
}

@Component("fooImpl1")
@Profile("!dev")
public class FooImpl1 implements Foo {
  @Override
  public void fooToYou() {}
}

@Component("fooImpl2")
@Profile("!dev")
public class FooImpl2 implements Foo {
  @Override
  public void fooToYou() {}
}

@Component("fooImpl3")
@Profile("dev")
public class FooImpl3 implements Foo {
  @Override
  public void fooToYou() {}
}

public class Service1 {
  @Autowired
  public void Service1(@Qualifier("fooImpl1") Foo foo) {}
}

public class Service2 {
  @Autowired
  public void Service1(@Qualifier("fooImpl2") Foo foo) {}
}

我想出了一種方法來完成我最終試圖通過刪除限定符並使用 inheritance 來完成的工作。

interface BaseFoo {
  public void fooToYou();
}

interface Foo1 extends BaseFoo {}
interface Foo2 extends BaseFoo {}

@Profile("!dev")
class FooImpl1 implements Foo1 {
  @Override
  public void fooToYou() {}
}

@Profile("!dev")
class FooImpl2 implements Foo2 {
  @Override
  public void fooToYou() {}
}

class BaseDevFooImpl implements BaseFoo {
  @Override
  public void fooToYou() {}
}

@Profile("dev")
class DevFooImpl1 extends BaseDevFooImpl implements Foo1 {}

@Profile("dev")
class DevFooImpl2 extends BaseDevFooImpl implements Foo2 {}

class Service1 {
  @Autowired
  public void Service1(Foo1 foo) {}
}

class Service2 {
  @Autowired
  public void Service1(Foo2 foo) {}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM