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

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

There is a newer version: 2023.2.5
Show newest version
package shz.spring.yaml;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.io.Resource;
import shz.core.NullHelp;
import shz.spring.BeanContainer;

class YamlInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor, Ordered {
    @Override
    public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
        Yaml yaml = beanClass.getAnnotation(Yaml.class);
        if (yaml != null) {
            Resource resource = BeanContainer.getResource(yaml.value());
            return NullHelp.isBlank(yaml.key())
                    ? YamlHelp.load(beanClass, resource)
                    : YamlHelp.get(beanClass, YamlHelp.load(resource), yaml.key());
        }
        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy