Commit 4403865f authored by Dmitriy Novikov's avatar Dmitriy Novikov
Browse files

Added RequestScopeAutowireCandidateResolver

parent ffb14454
package org.opengroup.osdu.indexer.di;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.AutowireCandidateResolver;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.stereotype.Component;
/**
* Custom {@link AutowireCandidateResolver} resolves undesirable beans as 'not autowire
* candidates'.
*/
@Component
@Slf4j
public class RequestScopeAutowireCandidateResolver extends
ContextAnnotationAutowireCandidateResolver {
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
String fullName = bdHolder.getBeanDefinition().getBeanClassName();
if (fullName != null && (
isTenantInfoFactory(fullName) ||
isAuditLogger(fullName) ||
isDpsHeaderFactory(fullName) ||
isJaxRsDpsLog(fullName))) {
log.info(
"Excluded undesirable bean from context: {} \n with scope: {} \n as autowire candidate for: {}",
fullName, bdHolder.getBeanDefinition().getScope(),
descriptor.getMember().getDeclaringClass().getName());
return false;
} else {
return super.isAutowireCandidate(bdHolder, descriptor);
}
}
private boolean isTenantInfoFactory(String fullName) {
return fullName.contains("org.opengroup.osdu.core.gcp.multitenancy.TenantFactory");
}
private boolean isAuditLogger(String fullName) {
return fullName.contains("org.opengroup.osdu.indexer.logging.AuditLogger");
}
private boolean isDpsHeaderFactory(String fullName) {
return fullName.contains("org.opengroup.osdu.core.common.http.DpsHeaderFactory");
}
private boolean isJaxRsDpsLog(String fullName) {
return fullName.contains("org.opengroup.osdu.core.common.logging.JaxRsDpsLog");
}
}
/*
* Copyright 2022 Google LLC
* Copyright 2022 EPAM Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.indexer.di;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AutowireCandidateResolver;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
/**
* `BeanFactoryPostProcessor` responsible for setting {@link RequestScopeAutowireCandidateResolver}
* as {@link AutowireCandidateResolver} for {@link DefaultListableBeanFactory} with purpose to add
* custom autowire candidate choosing logic.
*/
@Component
@Slf4j
public class RequestScopeAutowireCandidateResolverConfigurer implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(@NotNull ConfigurableListableBeanFactory factory)
throws BeansException {
Assert.state(factory instanceof DefaultListableBeanFactory,
"BeanFactory needs to be a DefaultListableBeanFactory");
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) factory;
beanFactory.setAutowireCandidateResolver(
new RequestScopeAutowireCandidateResolver()
);
log.info("RequestScopeAutowireCandidateResolverConfigurer was set for factory: {}",
factory.getClass().toString());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment