refactor(config): remove redundant Spring Boot config and fix injection patterns (#314)
* refactor(config): remove redundant Spring Boot config and fix injection patterns - Delete TransactionConfig (Spring Boot auto-configures JpaTransactionManager) - Delete MultipartConfig (replaced with spring.servlet.multipart.* properties) - Delete BeanConfig (RestTemplate moved to RestClientConfig, WS stats to WebSocketConfig) - Remove duplicate @EnableScheduling from TaskSchedulerConfig - Fix @ConfigurationProperties: remove @Component from AppProperties, change @Configuration to @ConfigurationProperties on BookmarkProperties - Move RestTemplate bean to RestClientConfig - Add WS stats suppression to WebSocketConfig via ApplicationReadyEvent - Remove @Autowired from KepubConversionService, GoogleParser, HardcoverBookSearchService (single-constructor injection) - Remove redundant two-arg constructor from GoogleParser - Fix CbxReaderService pre-existing compile error (transferEntryTo) * refactor(config): add primary RestTemplate bean and include audiobook settings in persistence configuration
This commit is contained in:
@@ -6,11 +6,12 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
import org.booklore.config.AppProperties;
|
||||||
import org.booklore.config.BookmarkProperties;
|
import org.booklore.config.BookmarkProperties;
|
||||||
|
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@EnableAsync
|
@EnableAsync
|
||||||
@EnableConfigurationProperties(BookmarkProperties.class)
|
@EnableConfigurationProperties({AppProperties.class, BookmarkProperties.class})
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class BookloreApplication {
|
public class BookloreApplication {
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package org.booklore.config;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
@ConfigurationProperties(prefix = "app")
|
@ConfigurationProperties(prefix = "app")
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
package org.booklore.config;
|
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
import org.springframework.web.socket.config.WebSocketMessageBrokerStats;
|
|
||||||
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class BeanConfig {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WebSocketMessageBrokerStats webSocketMessageBrokerStats;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RestTemplate restTemplate(HttpClient httpClient) {
|
|
||||||
JdkClientHttpRequestFactory factory = new JdkClientHttpRequestFactory(httpClient);
|
|
||||||
factory.setReadTimeout(Duration.ofSeconds(15));
|
|
||||||
return new RestTemplate(factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
webSocketMessageBrokerStats.setLoggingPeriod(30 * 24 * 60 * 60 * 1000L); // 30 days
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,9 +2,7 @@ package org.booklore.config;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ConfigurationProperties(prefix = "bookmarks")
|
@ConfigurationProperties(prefix = "bookmarks")
|
||||||
@Data
|
@Data
|
||||||
public class BookmarkProperties {
|
public class BookmarkProperties {
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package org.booklore.config;
|
|
||||||
|
|
||||||
import jakarta.servlet.MultipartConfigElement;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.util.unit.DataSize;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class MultipartConfig {
|
|
||||||
|
|
||||||
private static final long DEFAULT_MAX_UPLOAD_SIZE_MB = 1024;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides a MultipartConfigElement with a generous default max upload size.
|
|
||||||
* The actual user-configured limit from app_settings is enforced at the service layer.
|
|
||||||
* This bean is created during servlet container initialization (before Flyway migrations run),
|
|
||||||
* so it must NOT query the database.
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public MultipartConfigElement multipartConfigElement() {
|
|
||||||
long maxSizeBytes = DataSize.ofMegabytes(DEFAULT_MAX_UPLOAD_SIZE_MB).toBytes();
|
|
||||||
return new MultipartConfigElement("", maxSizeBytes, maxSizeBytes, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,10 +2,13 @@ package org.booklore.config;
|
|||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.net.http.HttpClient;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -17,6 +20,14 @@ public class RestClientConfig {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public RestTemplate restTemplate(HttpClient httpClient) {
|
||||||
|
JdkClientHttpRequestFactory factory = new JdkClientHttpRequestFactory(httpClient);
|
||||||
|
factory.setReadTimeout(Duration.ofSeconds(15));
|
||||||
|
return new RestTemplate(factory);
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RestTemplate oidcRestTemplate() {
|
public RestTemplate oidcRestTemplate() {
|
||||||
var factory = new SimpleClientHttpRequestFactory();
|
var factory = new SimpleClientHttpRequestFactory();
|
||||||
@@ -25,4 +36,3 @@ public class RestClientConfig {
|
|||||||
return new RestTemplate(factory);
|
return new RestTemplate(factory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableScheduling
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class TaskSchedulerConfig {
|
public class TaskSchedulerConfig {
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
package org.booklore.config;
|
|
||||||
|
|
||||||
import jakarta.persistence.EntityManagerFactory;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableTransactionManagement
|
|
||||||
public class TransactionConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
|
||||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,13 +2,16 @@ package org.booklore.config;
|
|||||||
|
|
||||||
import org.booklore.config.security.interceptor.WebSocketAuthInterceptor;
|
import org.booklore.config.security.interceptor.WebSocketAuthInterceptor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.messaging.simp.config.ChannelRegistration;
|
import org.springframework.messaging.simp.config.ChannelRegistration;
|
||||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||||
import org.springframework.scheduling.TaskScheduler;
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
|
import org.springframework.web.socket.config.WebSocketMessageBrokerStats;
|
||||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||||
@@ -29,6 +32,12 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
|||||||
this.env = env;
|
this.env = env;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
public void suppressStatsLogging(ApplicationReadyEvent event) {
|
||||||
|
var stats = event.getApplicationContext().getBean(WebSocketMessageBrokerStats.class);
|
||||||
|
stats.setLoggingPeriod(30 * 24 * 60 * 60 * 1000L); // 30 days
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||||
registry.enableSimpleBroker("/queue", "/topic")
|
registry.enableSimpleBroker("/queue", "/topic")
|
||||||
|
|||||||
+6
@@ -259,10 +259,16 @@ public class SettingPersistenceHelper {
|
|||||||
.maxFileSizeInMb(250)
|
.maxFileSizeInMb(250)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
MetadataPersistenceSettings.FormatSettings audiobookSettings = MetadataPersistenceSettings.FormatSettings.builder()
|
||||||
|
.enabled(false)
|
||||||
|
.maxFileSizeInMb(250)
|
||||||
|
.build();
|
||||||
|
|
||||||
MetadataPersistenceSettings.SaveToOriginalFile saveToOriginalFile = MetadataPersistenceSettings.SaveToOriginalFile.builder()
|
MetadataPersistenceSettings.SaveToOriginalFile saveToOriginalFile = MetadataPersistenceSettings.SaveToOriginalFile.builder()
|
||||||
.epub(epubSettings)
|
.epub(epubSettings)
|
||||||
.pdf(pdfSettings)
|
.pdf(pdfSettings)
|
||||||
.cbx(cbxSettings)
|
.cbx(cbxSettings)
|
||||||
|
.audiobook(audiobookSettings)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return MetadataPersistenceSettings.builder()
|
return MetadataPersistenceSettings.builder()
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package org.booklore.service.kobo;
|
package org.booklore.service.kobo;
|
||||||
|
|
||||||
import org.booklore.util.FileService;
|
import org.booklore.util.FileService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -11,10 +11,10 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class KepubConversionService {
|
public class KepubConversionService {
|
||||||
|
|
||||||
@Autowired
|
private final FileService fileService;
|
||||||
private FileService fileService;
|
|
||||||
|
|
||||||
public File convertEpubToKepub(File epubFile, File tempDir, boolean forceEnableHyphenation) throws IOException, InterruptedException {
|
public File convertEpubToKepub(File epubFile, File tempDir, boolean forceEnableHyphenation) throws IOException, InterruptedException {
|
||||||
validateInputs(epubFile);
|
validateInputs(epubFile);
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import org.booklore.model.enums.MetadataProvider;
|
|||||||
import org.booklore.service.appsettings.AppSettingService;
|
import org.booklore.service.appsettings.AppSettingService;
|
||||||
import org.booklore.util.BookUtils;
|
import org.booklore.util.BookUtils;
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
import tools.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
@@ -48,11 +47,6 @@ public class GoogleParser implements BookParser {
|
|||||||
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
|
private static final String GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
|
||||||
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public GoogleParser(ObjectMapper objectMapper, AppSettingService appSettingService) {
|
|
||||||
this(objectMapper, appSettingService, HttpClient.newHttpClient());
|
|
||||||
}
|
|
||||||
|
|
||||||
public GoogleParser(ObjectMapper objectMapper, AppSettingService appSettingService, HttpClient httpClient) {
|
public GoogleParser(ObjectMapper objectMapper, AppSettingService appSettingService, HttpClient httpClient) {
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.appSettingService = appSettingService;
|
this.appSettingService = appSettingService;
|
||||||
|
|||||||
-2
@@ -2,7 +2,6 @@ package org.booklore.service.metadata.parser.hardcover;
|
|||||||
|
|
||||||
import org.booklore.service.appsettings.AppSettingService;
|
import org.booklore.service.appsettings.AppSettingService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -27,7 +26,6 @@ public class HardcoverBookSearchService {
|
|||||||
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
||||||
private final AtomicLong successCount = new AtomicLong(0);
|
private final AtomicLong successCount = new AtomicLong(0);
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public HardcoverBookSearchService(AppSettingService appSettingService) {
|
public HardcoverBookSearchService(AppSettingService appSettingService) {
|
||||||
this.appSettingService = appSettingService;
|
this.appSettingService = appSettingService;
|
||||||
this.restClient = RestClient.builder()
|
this.restClient = RestClient.builder()
|
||||||
|
|||||||
@@ -43,7 +43,10 @@ spring:
|
|||||||
fail-on-null-for-primitives: false
|
fail-on-null-for-primitives: false
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
enabled: false
|
enabled: true
|
||||||
|
max-file-size: 1024MB
|
||||||
|
max-request-size: 1024MB
|
||||||
|
file-size-threshold: 0
|
||||||
mvc:
|
mvc:
|
||||||
async:
|
async:
|
||||||
request-timeout: 600000
|
request-timeout: 600000
|
||||||
|
|||||||
Reference in New Issue
Block a user