我有拦截RestTemplate的逻辑,我在配置文件(SecurityConfiguration.java)中添加/注册这个RestTemplate,但是我想通过获取已经注册的RestTemplate对象从另一个配置文件中添加拦截器:
public class TranslogRestTemplateCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean isRestTemplate = false;
try {
if (context.getBeanFactory() != null) {
isRestTemplate = (context.getBeanFactory().getBean(RestTemplate.class) != null);
}
} catch (BeansException e) {
return false;
}
return isRestTemplate;
}
}配置类:
@Configuration
public class RestTemplateConfig {
private final MyInterceptor myInterceptor;
@Value("${com.pqr.you.rest.enabled:true}")
private boolean transEnabled;
@Autowired
public RestTemplateConfig(MyInterceptor myInterceptor) {
this.myInterceptor = myInterceptor;
}
@Autowired
private ApplicationContext appContext;
// The logic added below is not working for me
@Bean
@Conditional(TranslogRestTemplateCondition.class)
public RestTemplate addInterceptor(ApplicationContext appContext) {
RestTemplate restTemplate = appContext.getBean(RestTemplate.class);
if (transEnabled) {
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add(myInterceptor);
restTemplate.setInterceptors(interceptors);
}
return restTemplate;
}
}RestTemplate的实际逻辑(它将与所需的拦截器一起返回)和一些其他值(在返回此restTemplate时,我的拦截器也需要在这里添加,而不超过现有值)或,方法是接受下面的restTempalte对象并将MyInterceptor添加到restTemplate.中。
@Configuration
public class SecurityConfiguration {
@Bean
public AbcInterceptor abcRequestInterceptor(XyzService xyzService) {
return new AbcInterceptor("abc-app", null, xyzService);
}
// I dont want to create bean here
/*@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}*/
@Bean
public RestTemplate restTemplate(AbcRequestInterceptor abcRequestInterceptor) {
RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(abcRequestInterceptor);
//interceptors.add(myInterceptor); // I dont want to add this interceptor here
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}发布于 2018-02-07 11:41:15
我认为在这种情况下您需要的是一个BeanPostProcessor,而不是一个Condition类。如果bean存在,它将允许您修改它。您可以创建如下所示的类,而不是RestTemplateConfiguration类和Condition
@Configuration
public class RestTemplatePostProcessor implements BeanPostProcessor {
@Value("${com.pqr.you.rest.enabled:true}")
private boolean transEnabled;
@Bean
MyInterceptor myInterceptor() {
return new MyInterceptor();
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RestTemplate) { // or you can check by beanName if you like
final RestTemplate restTemplate = (RestTemplate) bean;
if (transEnabled) {
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add(myInterceptor());
restTemplate.setInterceptors(interceptors);
}
return restTemplate;
}
return bean;
}
}注意,在您的代码中,您正在尝试创建另一个RestTemplate实例(名为"addInterceptor"),这似乎是不想要的。
https://stackoverflow.com/questions/48625268
复制相似问题