All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.spring.yaml.YamlInitializer Maven / Gradle / Ivy

The newest version!
package shz.spring.yaml;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import shz.core.FieldSetter;
import shz.core.FileHelp;
import shz.core.IOHelp;
import shz.core.NullHelp;
import shz.spring.BeanContainer;

import java.io.BufferedReader;
import java.io.File;
import java.util.Map;

final class YamlInitializer implements ApplicationContextInitializer, Ordered {
    @Override
    public void initialize(ConfigurableApplicationContext context) {
        BeanContainer.set(Integer.MIN_VALUE, () -> {
            Map yamlBeans = BeanContainer.getContext().getBeansWithAnnotation(Yaml.class);
            if (NullHelp.nonEmpty(yamlBeans)) {
                yamlBeans.forEach((name, bean) -> {
                    Class cls = bean.getClass();
                    Yaml yaml = cls.getAnnotation(Yaml.class);
                    File file = FileHelp.fromCls(cls, yaml.path());
                    if (file == null) return;
                    BufferedReader br = IOHelp.getBr(file);
                    Object loadBean;
                    if (NullHelp.isBlank(yaml.key())) loadBean = YamlHelp.load(cls, br);
                    else loadBean = YamlHelp.get(cls, YamlHelp.load(br), yaml.key());
                    FieldSetter.copy(loadBean, bean);
                });
            }
        });
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy