gpt4 book ai didi

java - 将 bean 放入 map 中,以按名称从工厂获取 bean

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

我需要按名称从工厂获取 bean。我想知道是否有更优雅的方法来处理这个问题?我的工作代码现在看起来像这样。这是我的接口(interface)服务和“工厂”

public interface GreetingService {
void getGreeting(String name);
}

public interface GreetingServiceFactory {
GreetingService getGreetingService(String region);
}

实现greetingService:

  @Service
public class EnglishGreetingServiceImpl implements GreetingService {
@Override
public void getGreeting(String name) {
System.out.println("Hello " + name);
}
}

@Service
public class GermanGreetingServiceImpl implements GreetingService {
@Override
public void getGreeting(String name) {
System.out.println("Willkommen " + name);
}
}

实现工厂:

@Service
public class GreetingServiceFactoryImpl implements GreetingServiceFactory {

private Map<String, GreetingService> greetingBeanMap;
@Autowired
@Qualifier("germanGreetingServiceImpl")
private GreetingService germanGreetingService;
@Autowired
@Qualifier("englishGreetingServiceImpl")
private GreetingService englishGreetingService;
@PostConstruct
public void init () {
greetingBeanMap = new HashMap<>();
greetingBeanMap.put("en", englishGreetingService);
greetingBeanMap.put("de", germanGreetingService);

}
@Override
public GreetingService getGreetingService(String region) {
return greetingBeanMap.get(region);
}
}

带有示例代码的主类,我在某个名称后收到 bean

@SpringBootApplication
public class SpringFactoryApplication implements CommandLineRunner {

@Autowired
private GreetingServiceFactory greetingServiceFactory;

public static void main(String[] args) {
SpringApplication.run(SpringFactoryApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
String config1 = "en";
GreetingService english = greetingServiceFactory.getGreetingService(config1);
english.getGreeting("John");

String config2 = "de";
GreetingService deutsch = greetingServiceFactory.getGreetingService(config2);
deutsch.getGreeting("Hans");
}
}

最佳答案

在你上面的代码中,这段代码是完全多余的

 @Autowired
@Qualifier("germanGreetingServiceImpl")
private GreetingService germanGreetingService;

@Autowired
@Qualifier("englishGreetingServiceImpl")
private GreetingService englishGreetingService;

@PostConstruct
public void init () {
greetingBeanMap = new HashMap<>();
greetingBeanMap.put("en", englishGreetingService);
greetingBeanMap.put("de", germanGreetingService);
}

这段代码可以替换为

@Autowired
private Map<String, GreetingService> greetingBeanMap;

当你这样声明时,spring将搜索GreetingService接口(interface)的所有实现并注入(inject)到你的map中,键作为bean名称。即,greetingBeanMap 的键将是 germanGreetingServiceImplenglishGreetingServiceImpl,值将是 bean 本身。

如果您想让键为“en”和“de”而不是bean名称,那么您可以将bean命名为“en”和“de”。像这样

@Service("en")
public class EnglishGreetingServiceImpl implements GreetingService {
......
}

@Service("de")
public class GermanGreetingServiceImpl implements GreetingService {
......
}

关于java - 将 bean 放入 map 中,以按名称从工厂获取 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49097519/

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