Şu satırı dahil ederiz
getLocale metoduimport org.springframework.context.i18n.LocaleContextHolder;
Şöyle yaparız
Locale locale = LocaleContextHolder.getLocale();
getLocale metoduimport org.springframework.context.i18n.LocaleContextHolder;
Locale locale = LocaleContextHolder.getLocale();
Spring Boot offers two alternatives to create native binaries:1. A system-dependent binary: this approach requires a local GraalVM installation with the native-image extension. It will create a non-cross-platform system-dependent binary.For this, Spring Boot has a dedicated profile:./mvnw -Pnative package2. A Docker image: this approach builds a containerized version of the application. It requires a local image build, e.g., Docker. Internally, it leverages CNCF Buildpacks (but doesn’t require pack).Spring Boot provides a Maven target for this:./mvnw spring-boot:native-image
The Spring AOT plugin will automatically be run in the build pipeline to create a special Spring Boot JAR which is needed to run when compiled to a native image. It will try to create all needed reachability configurations for your program to work correctly as a native image.reflection-config.json Dosyası
<dependency><groupId>org.springframework.experimental</groupId><artifactId>spring-native</artifactId><version>${spring-native.version}</version></dependency><plugin><groupId>org.springframework.experimental</groupId><artifactId>spring-aot-maven-plugin</artifactId><version>${spring-native.version}</version><executions><execution><id>test-generate</id><goals><goal>test-generate</goal></goals></execution><execution><id>generate</id><goals><goal>generate</goal></goals></execution></executions></plugin>
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'org.springframework.experimental.aot' version '0.11.3'
}
group = 'com.springnative.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
maven { url 'https://repo.spring.io/release' }
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
tasks.named('bootBuildImage') {
builder = 'paketobuildpacks/builder:tiny'
environment = ['BP_NATIVE_IMAGE': 'true']
}./gradlew nativeRun
$ mvn clean package$ mvn spring-boot:build-image$ docker run --name spring-native-example -p 8080:8080 spring-native-example:0.0.1-SNAPSHOT
//Build a normal Spring Boot image mvn spring-boot:build-image -DskipTests //Build a Spring Boot native image mvn -Pnative spring-boot:build-image -DskipTests
./mvnw spring-boot:native-image
@SpringBootApplication
@NativeHint(options = ["--enable-https"]) <1>
@TypeHint(
types = [
Model::class, Data::class, Result::class, Thumbnail::class,
Collection::class, Resource::class, Url::class, URI::class
],
access = AccessBits.FULL_REFLECTION <2>
)
class BootNativeApplication {...}@TypeHint( types = [News::class], access = [TypeAccess.DECLARED_CONSTRUCTORS, TypeAccess.PUBLIC_METHODS] ) @SpringBootApplication class SpringBootKotlinReactiveApplication
Separate databaseSeparate schemaShared schema
public abstract class TenantContext {public static final String DEFAULT_TENANT_ID = "public";private static ThreadLocal<String> currentTenant = new ThreadLocal<>();public static void setCurrentTenant(String tenant) {currentTenant.set(tenant);}public static String getCurrentTenant() {return currentTenant.get();}public static void clear() {currentTenant.remove();}}
In the earlier versions of Spring Boot, we could extend the org.springframework.web.servlet.handler.HandlerInterceptorAdapter class, but in newer versions, the class is deprecated,...
@Componentpublic class TenantRequestInterceptor implements AsyncHandlerInterceptor {private SecurityDomain securityDomain;public TenantRequestInterceptor(SecurityDomain securityDomain) {this.securityDomain = securityDomain;}@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) {return Optional.ofNullable(request).map(req -> securityDomain.getTenantIdFromJwt(req)).map(tenant -> setTenantContext(tenant)).orElse(false);}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) {TenantContext.clear();}private boolean setTenantContext(String tenant) {TenantContext.setCurrentTenant(tenant);return true;}}
@Configurationpublic class WebConfiguration implements WebMvcConfigurer {@Autowiredprivate TenantRequestInterceptor tenantInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(tenantInterceptor).addPathPatterns("/**");}}
protected ConnectionProvider getAnyConnectionProvider();protected ConnectionProvider selectConnectionProvider(String tenantIdentifier);
public class SchemaMultiTenantConnectionProvider extends
AbstractMultiTenantConnectionProvider {
public static final String HIBERNATE_PROPERTIES_PATH = "/hibernate-%s.properties";
private final Map<String, ConnectionProvider> connectionProviderMap;
public SchemaMultiTenantConnectionProvider() {
this.connectionProviderMap = new HashMap<String, ConnectionProvider>();
}
@Override
protected ConnectionProvider getAnyConnectionProvider() {
return getConnectionProvider(TenantContext.DEFAULT_TENANT_ID);
}
@Override
protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) {
return getConnectionProvider(tenantIdentifier);
}
private ConnectionProvider getConnectionProvider(String tenantIdentifier) {
return Optional.ofNullable(tenantIdentifier)
.map(connectionProviderMap::get)
.orElseGet(() -> createNewConnectionProvider(tenantIdentifier));
}
private ConnectionProvider createNewConnectionProvider(String tenantIdentifier) {
return Optional.ofNullable(tenantIdentifier)
.map(this::createConnectionProvider)
.map(connectionProvider -> {
connectionProviderMap.put(tenantIdentifier, connectionProvider);
return connectionProvider;
})
.orElseThrow(() ->
new ConnectionProviderException(
String.format("Cannot create new connection provider
for tenant: %s", tenantIdentifier))
);
}
private ConnectionProvider createConnectionProvider(String tenantIdentifier) {
return Optional.ofNullable(tenantIdentifier)
.map(this::getHibernatePropertiesForTenantId)
.map(this::initConnectionProvider)
.orElse(null);
}
private Properties getHibernatePropertiesForTenantId(String tenantId) {
try {
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream(
String.format(HIBERNATE_PROPERTIES_PATH, tenantId)));
return properties;
} catch (IOException e) {
throw new RuntimeException(
String.format("Cannot open hibernate properties: %s", HIBERNATE_PROPERTIES_PATH));
}
}
private ConnectionProvider initConnectionProvider(Properties hibernateProperties) {
DriverManagerConnectionProviderImpl connectionProvider =
new DriverManagerConnectionProviderImpl();
connectionProvider.configure(hibernateProperties);
return connectionProvider;
}
}The only thing we have to implement in this class are the methods getAnyConnectionProvider() and selectConnectionProvider(String tenantId).The first method sets up a connection to the database when the tenantId is not set. This happens when the application is starting. Validating whether the tenant is set can be implemented in AsyncHandlerInterceptor on the Spring side and throws an exception when it’s not set or is incorrect.The second method is responsible for returning the appropriate ConnectionProvider for the indicated tenantId. The solution assumes that we collect ConnectionProvider on a map so as not to create a new one every time. If it’s not on the map yet, then we create a new one and add it to the map. Of course, this can be moved to the classic cache, where you can additionally manage lifetime (TTL).
@WebFluxTest(controllers = {RateRestController.class})@Tag("UnitTest")public class RateRestControllerTest {@MockBeanprivate RateService rateService;@AutowiredWebTestClient webTestClient;@Testpublic void getLatestRates() throws Exception {// Mock return data of rate servicewhen(rateService.fetchLatestRates(anyString())).thenAnswer(invocation -> {String baseCurrency = (String) invocation.getArgument(0);LocalDateTime timestamp = LocalDateTime.now();return Flux.just(new Rate(timestamp, baseCurrency, "USD", Math.random()),new Rate(timestamp, baseCurrency, "EUR", Math.random()),new Rate(timestamp, baseCurrency, "CAD", Math.random()),new Rate(timestamp, baseCurrency, "JPY", Math.random()));});// trigger API request to rate controllerwebTestClient.get().uri("/rates/latest/GBP").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectBody().jsonPath("$").isArray().jsonPath("$[0].baseCurrency").isEqualTo("GBP").jsonPath("$[0].counterCurrency").isEqualTo("USD").jsonPath("$[0].rate").isNumber().jsonPath("$[1].baseCurrency").isEqualTo("GBP").jsonPath("$[1].counterCurrency").isEqualTo("EUR").jsonPath("$[1].rate").isNumber().jsonPath("$[2].baseCurrency").isEqualTo("GBP").jsonPath("$[2].counterCurrency").isEqualTo("CAD").jsonPath("$[2].rate").isNumber().jsonPath("$[3].baseCurrency").isEqualTo("GBP").jsonPath("$[3].counterCurrency").isEqualTo("JPY").jsonPath("$[3].rate").isNumber();}}
import org.springframework.context.MessageSource;
MessageSource is an interface that defines several methods for resolving messages. The ApplicationContext interface extends this interface so that all application contexts are able to resolve text messages.
Spring Boot has i18n built-in thanks to the Spring Framework and its MessageSource implementations. There’s a ResourceBundleMessageSource that builds on ResourceBundle, as well as a ReloadableResourceBundleMessageSource that should be self-explanatory.
Inject MessageSource into a Spring bean and call getMessage(key, args, locale) to your heart’s content!
When you build your spring boot application and make use of Kafka in order to create some consumer, Spring provides on its own a listener container for asynchronous execution of POJO listeners. The provided listener container has three ways to handle a potential exception:1. Ignores it and moves to the next record.2. It can retry to process the same item from the listed topics/partitions of that listener.3. It can send the item to a dead letter topic.
By default, records that fail are simply logged, and we move on to the next one. We can, however, configure an error handler in the listener container to perform some other action.
The Java Kafka client library offers stateless retry, with the Kafka consumer retrying a retryable exception as part of the consumer poll.- Retries happen within the consumer poll for the batch.
- Consumer poll must complete before poll timeout, containing all retries, and total processing time (including REST calls & DB calls), retry delay and backoff, for all records in the batch.
- Default poll time is 5 minutes for 500 records in the batch. This only averages to 600ms per event.
- If poll time is exceeded this results in event duplication.
- Calculation of retries/time possible, but total retry duration will have to be short.
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.retry.support.RetryTemplate;
//Stateless retry listener.
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String>
kafkaStatelessRetryListenerContainerFactory(
ConsumerFactory<String, String> consumerFactory, final RetryTemplate retryTemplate) {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(consumerFactory);
factory.setRetryTemplate(retryTemplate);
factory.setRecoveryCallback((context -> {
log.warn("**** Retries exhausted - error class: "+context.getLastThrowable() +
" - error message: "+context.getLastThrowable().getMessage());
// Return null to mark processing complete.
return null;
}));
return factory;
}
@Bean
public RetryTemplate retryTemplate() {
return RetryTemplate.builder()
.fixedBackoff(4000)
.maxAttempts(5)
.build();
}According to the documentation, there are two interfaces responsible for naming your tables, columns etc. in Hibernate: ImplicitNamingStrategy and PhysicalNamingStrategy.
ImplicitNamingStrategy is in charge of naming all objects that were not explicitly named by a developer: e.g. entity name, table name, column name, index, FK etc. The resulting name is called the logical name, it is used internally by Hibernate to identify an object. It is not the name that gets put into the DB.
spring.jpa.hibernate.naming.physical-strategy=
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImplspring:jpa:hibernate:naming:physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImplimplicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
PhysicalNamingStrategy provides the actual physical name used in the DB based on the logical JPA object name. Effectively, this means that using Hibernate you cannot specify database object names directly, but only logical ones.
Effectively, this means that using Hibernate you cannot specify database object names directly, but only logical ones.
Spring Boot overrides Hibernate default implementations for both interfaces and uses SpringImplicitNamingStrategy and SpringPhysicalNamingStrategy instead.
Effectively, SpringImplicitNamingStrategy copies the behaviour of ImplicitNamingStrategyJpaCompliantImpl with only a minor difference in join table naming....By default, Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy. This implementation provides the same table structure as Hibernate 4: all dots are replaced by underscores and camel casing is replaced by underscores as well. Additionally, by default, all table names are generated in lower case. For example, a TelephoneNumber entity is mapped to the telephone_number table....Basically, it always transforms camelCase and PascalCase to snake_case. In fact, using it isn't possible to work with non_snake_case at all.
By default, Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy. Which does this: For example, a TelephoneNumber entity is mapped to the telephone_number table (same goes for columns).
The JPA default table name is the name of the class (minus the package) with the first letter capitalized.
@Table(name = "PetType") versek bile veri tabanında pet_type isimli tablo oluşur@Table(name = "\"PetType\"") versek bile veri tabanında "pet_type" isimli tablo oluşur
Kullanmak için şöyle yaparız.public class UpperCaseNamingStrategy extends SpringPhysicalNamingStrategy {@Overrideprotected Identifier getIdentifier(String name, boolean quoted,
JdbcEnvironment jdbcEnvironment) {return new Identifier(name.toUpperCase(), quoted);}}
spring.jpa.hibernate.naming.physical-strategy=
com.baeldung.namingstrategy.UpperCaseNamingStrategy