策略模式
2025/10/25小于 1 分钟约 92 字
策略模式
1.spring serviceLocator 实现
1.建立定位器
@Configuration
@slf4j
public class ServiceLocatorConfig{
/**
* 创建一个XXX定位器
*/
@Bean
public FactoryBean serviceLocatorFactoryBean(){
ServiceLocatorFactoryBean serviceLocatorFactoryBean = new ServiceLocatorFactoryBean();
serviceLocatorFactoryBean.setServiceLocatorInterface(XXXLocator.class);
return serviceLocatorFactoryBean;
}
public interface XXXLocator {
MessageSendService getMessageSender(String senderType);
}
}2.使用
public class Test{
@Autowired
private XXXLocator locator;
public void test(){
var sender = locator.getMessageSender("email");
sender.send();
}
}2.springboot 动态注入 如list map等
