package com.manager.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.util.Map;
@Slf4j
@Component
public class BeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
public static <T> T getBean(Class<T> clazz) {
if (applicationContext == null) {
throw new RuntimeException("applicationContext获取失败");
}
return applicationContext.getBean(clazz);
}
}
public class FileUtils {
public static final OssComponent OSS_COMPONENT;
static {
OSS_COMPONENT = ContextUtils.getBean(OssComponent.class);
}
public static String fileWriteOut(String fileName, Long attachmentsId, InputStream inputStream, Integer source, Long companyCode) {
fileUrl = OSS_COMPONENT.uploadFile(dir, fileName, attachmentsId.toString(), inputStream, AttachmentUtils.contentType(fileName));
}
}
调用工具类直接 FileUtils.fileWriteOut()即可