init
This commit is contained in:
110
src/main/java/com/newyetai/common/CommonConfiguration.java
Normal file
110
src/main/java/com/newyetai/common/CommonConfiguration.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package com.newyetai.common;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.newyetai.common.cache.DefaultCacheProvider;
|
||||
import com.newyetai.common.cache.ICacheProvider;
|
||||
import com.newyetai.common.log.ILogProvider;
|
||||
import com.newyetai.common.log.provider.Slf4jLogProvider;
|
||||
import com.newyetai.common.log.util.LogUtils;
|
||||
import com.newyetai.common.service.IMockProvider;
|
||||
import com.newyetai.common.service.IMockProvider.DefaultMockProvider;
|
||||
import com.newyetai.common.util.EnvironmentUtils;
|
||||
import com.newyetai.common.util.MockUtils;
|
||||
import com.newyetai.component.file.service.IFileStorage;
|
||||
import com.newyetai.component.file.service.storage.LocalFileStorage;
|
||||
import com.newyetai.component.file.util.FileStorageUtils;
|
||||
|
||||
import lombok.var;
|
||||
|
||||
/**
|
||||
* @author sr on 2020-07-24
|
||||
*/
|
||||
@Configuration
|
||||
public class CommonConfiguration {
|
||||
|
||||
@Bean
|
||||
EnvironmentUtils environmentUtils(Environment environment) {
|
||||
return new EnvironmentUtils(environment);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ICacheProvider.class)
|
||||
@Order(100)
|
||||
DefaultCacheProvider defauCacheProvider() {
|
||||
return new DefaultCacheProvider();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(100)
|
||||
@ConditionalOnMissingBean(ILogProvider.class)
|
||||
Slf4jLogProvider slf4jLogProvider() {
|
||||
return new Slf4jLogProvider();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(200)
|
||||
LogUtils logUtils(ILogProvider iLogProvider) {
|
||||
LogUtils logUtils = new LogUtils();
|
||||
logUtils.setLogProvider(iLogProvider);
|
||||
return logUtils;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(IFileStorage.class)
|
||||
public LocalFileStorage localFileStorage() {
|
||||
return new LocalFileStorage();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FileStorageUtils fileStorageUtils(IFileStorage fileStorage) {
|
||||
return new FileStorageUtils(fileStorage);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(IMockProvider.class)
|
||||
public IMockProvider defaultMockProvider() {
|
||||
return new DefaultMockProvider();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MockUtils mockUtils(IMockProvider mockProvider) {
|
||||
return new MockUtils(mockProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* requestbody string trim
|
||||
* @return
|
||||
* @author sr on 2024-04-19
|
||||
*/
|
||||
@Bean
|
||||
public SimpleModule stringDeserializerModule() {
|
||||
final var module = new SimpleModule();
|
||||
module.addDeserializer(String.class, new StringDeserializer() {
|
||||
@Override
|
||||
public String deserialize(JsonParser p, DeserializationContext ctx) throws java.io.IOException {
|
||||
final var value = super.deserialize(p, ctx);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final var trimmed = value.trim();
|
||||
if (trimmed.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return trimmed;
|
||||
}
|
||||
});
|
||||
return module;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user