springboot.ossclient.processor.OssClientComponentsProcessor Maven / Gradle / Ivy
/*
* @(#)OssClientComponentsProcessor.java 2023-09-10
*
* Copyright (c) 2023. All Rights Reserved.
*
*/
package springboot.ossclient.processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import com.github.javaclub.ossclient.OssConstants.Aliyun;
import com.github.javaclub.ossclient.OssConstants.LocalStorage;
import com.github.javaclub.ossclient.OssConstants.Minio;
import com.github.javaclub.ossclient.impl.AliyunOssClient;
import com.github.javaclub.ossclient.impl.LocalOssClient;
import com.github.javaclub.ossclient.impl.MinioOssClient;
/**
* OssClientComponentsProcessor
*
* @author Gerald Chen
* @version $Id: OssClientComponentsProcessor.java 2023-09-10 17:31:49 Exp $
*/
public class OssClientComponentsProcessor implements BeanDefinitionRegistryPostProcessor {
private static final Logger log = LoggerFactory.getLogger(OssClientComponentsProcessor.class);
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (!isInWebMvc()) {
log.warn("OssClientComponentsProcessor 注册失败: {}", "必须在web容器内");
return;
}
if (Aliyun.supported()) {
BeanDefinition aliyunOssBean = new RootBeanDefinition(AliyunOssClient.class);
registry.registerBeanDefinition(AliyunOssClient.class.getSimpleName().toLowerCase(), aliyunOssBean);
log.info("AliyunOssClient registered.");
}
if (Minio.supported()) {
BeanDefinition minioOssBean = new RootBeanDefinition(MinioOssClient.class);
registry.registerBeanDefinition(MinioOssClient.class.getSimpleName().toLowerCase(), minioOssBean);
log.info("MinioOssClient registered.");
}
if (LocalStorage.supported()) { // 已改为通过 @ComponentScan 方式自动注入
// BeanDefinition localAssetsBean = new RootBeanDefinition(AssetsPreviewController.class);
// registry.registerBeanDefinition("assetsPreviewController", localAssetsBean);
// log.info("LocalAssetsPreviewController registered.");
}
BeanDefinition localOssBean = new RootBeanDefinition(LocalOssClient.class);
registry.registerBeanDefinition(LocalOssClient.class.getSimpleName().toLowerCase(), localOssBean);
log.info("LocalOssClient registered.");
log.info("OssClientComponentsProcessor registry successfully.");
}
boolean isInWebMvc() {
try {
Class.forName(RequestMappingHandlerMapping.class.getName());
return true;
} catch (Throwable e) {
return false;
}
}
}