1 Ağustos 2023 Salı

Spring 3 İçin SpringNative @ImportRuntimeHints Anotasyonu

Giriş
Açıklaması şöyle
In Spring Native (as we did in Quarkus), we need to specify some classes for reflection, serialization, proxy usage, etc. Spring calls all of these <hints>. The problem is that the GraalVM, at compilation time, cannot recognize every class in our project that must be used for reflection at runtime. For this reason, these frameworks (Spring and Quarkus) have annotations that we can use con classes to specify reflection at compilation time to GraalVM.
Örnek
Şöyle yaparız
@SpringBootApplication
@ImportRuntimeHints(LdapServiceApplication.MyRuntimeHints.class)
public class LdapServiceApplication {

 public static void main(String[] args) {
  SpringApplication.run(LdapServiceApplication.class, args);
 }

 @Bean
 public RestTemplate restTemplate() {
  return new RestTemplate();
 }

 @PostConstruct
 void postConstruct() {
  System.setProperty("javax.net.ssl.trustStore", 
    System.getProperty("javax.net.ssl.trustStore"));
  System.setProperty("javax.net.ssl.trustStorePassword", 
    System.getProperty("javax.net.ssl.trustStorePassword"));
 }

 static class MyRuntimeHints implements RuntimeHintsRegistrar {

  @Override
  public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
   // Register serialization
   hints.serialization().registerType(HashMap.class).registerType(LinkedList.class);
   hints.reflection().registerType(TypeReference.of("javax.net.ssl.SSLSocketFactory"),
     builder -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS));
   hints.resources().registerPattern("db/migration/*.sql");
  }
}
Örnek
Şöyle yaparız
import org.postgresql.util.PGobject;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;

public class PostgresRuntimeHints implements RuntimeHintsRegistrar {

    @Override
    public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
        hints.reflection().registerType(PGobject.class, MemberCategory.values());
    }
}


import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.jdbcjobstore.JobStoreSupport;
import org.quartz.impl.jdbcjobstore.JobStoreTX;
import org.quartz.impl.jdbcjobstore.PostgreSQLDelegate;
import org.quartz.utils.HikariCpPoolingConnectionProvider;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;

public class QuartzRuntimeHints implements RuntimeHintsRegistrar {

  @Override
  public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
    hints.reflection().registerType(JobStoreSupport.class, MemberCategory.values());
    hints.reflection().registerType(JobStoreTX.class, MemberCategory.values());
    hints.reflection().registerType(StdSchedulerFactory.class, MemberCategory.values());
    hints.reflection().registerType(HikariCpPoolingConnectionProvider.class, MemberCategory.values());
    hints.reflection().registerType(PostgreSQLDelegate.class, MemberCategory.values());
  }
}

Hiç yorum yok:

Yorum Gönder