Spring Native注册反射元信息

实现RuntimeHintsRegistrar接口,再用hints添加要注册的类型。
eg:

@Configuration
@ImportRuntimeHints(MyRuntimeHintsRegistrar.class)
public class MyRuntimeHintsRegistrar implements RuntimeHintsRegistrar {

    /**
     * For GraalVM Native Compile
     *
     * @param hints
     * @param classLoader
     */
    @Override
    public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
        try {
            hints.reflection()
                    .registerType(java.text.Normalizer.class)
                    .registerType(java.text.Normalizer.Form.class)
                    .registerType(org.jasypt.salt.RandomSaltGenerator.class, MemberCategory.values())
                    .registerType(org.jasypt.iv.RandomIvGenerator.class, MemberCategory.values())
                    .registerType(org.jasypt.normalization.Normalizer.class)
                    .registerType(Class.forName("org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource"))
                    .registerType(Class.forName("org.springframework.core.env.PropertySource$StubPropertySource"))
            ;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

posted @ 2024-05-31 16:39  漠孤烟  阅读(148)  评论(0)    收藏  举报