Skip to content

Commit

Permalink
Merge pull request #254 from livk-cloud/reactor-dev
Browse files Browse the repository at this point in the history
refactor: ConversionService使用延迟加载
  • Loading branch information
livk-cloud committed Apr 30, 2024
2 parents 9e61235 + 8bcc3c4 commit e3ffb27
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.ConversionService;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
Expand All @@ -45,12 +46,12 @@ public class UserAgentConfiguration {

/**
* User agent helper user agent helper.
* @param applicationContext the application context
* @param conversionService the application context
* @return the user agent helper
*/
@Bean
public UserAgentHelper userAgentHelper(ApplicationContext applicationContext) {
return new UserAgentHelper(applicationContext);
public UserAgentHelper userAgentHelper(@Lazy ConversionService conversionService) {
return new UserAgentHelper(conversionService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package com.livk.context.useragent;

import com.livk.context.useragent.domain.UserAgent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.ConversionService;
import org.springframework.http.HttpHeaders;

Expand All @@ -27,26 +26,20 @@
*
* @author livk
*/
@RequiredArgsConstructor
public class UserAgentHelper {

private volatile ConversionService conversionService;
private final ConversionService conversionService;

private final ApplicationContext applicationContext;
public UserAgentHelper(@Lazy ConversionService conversionService) {
this.conversionService = conversionService;
}

/**
* Convert user agent.
* @param headers the headers
* @return the user agent
*/
public UserAgent convert(HttpHeaders headers) {
if (conversionService == null) {
synchronized (this) {
if (conversionService == null) {
conversionService = applicationContext.getBean(ConversionService.class);
}
}
}
return conversionService.convert(headers, UserAgent.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.http.HttpHeaders;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -53,7 +54,7 @@ void convertBrowscap() {
applicationContext = new AnnotationConfigApplicationContext(BrowscapConfig.class,
ConversionServiceConfig.class);

UserAgentHelper helper = new UserAgentHelper(applicationContext);
UserAgentHelper helper = new UserAgentHelper(applicationContext.getBean(ConversionService.class));
UserAgent userAgent = helper.convert(headers);
assertNotNull(userAgent);
assertEquals(userAgentStr, userAgent.userAgentStr());
Expand All @@ -72,7 +73,7 @@ void convertYauaa() {

applicationContext = new AnnotationConfigApplicationContext(YauaaConfig.class, ConversionServiceConfig.class);

UserAgentHelper helper = new UserAgentHelper(applicationContext);
UserAgentHelper helper = new UserAgentHelper(applicationContext.getBean(ConversionService.class));

UserAgent userAgent = helper.convert(headers);
assertNotNull(userAgent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.livk.context.useragent.yauaa.YauaaUserAgentConverter;
import nl.basjes.parse.useragent.UserAgentAnalyzer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
Expand All @@ -45,8 +44,8 @@ public ConversionService conversionService(ObjectProvider<Converter<?, ?>> conve
}

@Bean
public UserAgentHelper userAgentHelper(ApplicationContext applicationContext) {
return new UserAgentHelper(applicationContext);
public UserAgentHelper userAgentHelper(ConversionService conversionService) {
return new UserAgentHelper(conversionService);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.livk.context.useragent.yauaa.YauaaUserAgentConverter;
import nl.basjes.parse.useragent.UserAgentAnalyzer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
Expand All @@ -45,8 +44,8 @@ public ConversionService conversionService(ObjectProvider<Converter<?, ?>> conve
}

@Bean
public UserAgentHelper userAgentHelper(ApplicationContext applicationContext) {
return new UserAgentHelper(applicationContext);
public UserAgentHelper userAgentHelper(ConversionService conversionService) {
return new UserAgentHelper(conversionService);
}

}

0 comments on commit e3ffb27

Please sign in to comment.