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

net.optionfactory.hj.spring.SpringDriverLocator Maven / Gradle / Ivy

Go to download

Custom Hibernate types for serializing fields as JSON, using either jackson or gson

There is a newer version: 6.7
Show newest version
package net.optionfactory.hj.spring;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Optional;
import net.optionfactory.hj.JsonDriver;
import net.optionfactory.hj.JsonDriverLocator;
import net.optionfactory.hj.JsonDriverNotFound;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@Configurable
public class SpringDriverLocator implements JsonDriverLocator, ApplicationContextAware {

    private ApplicationContext ac;

    @Override
    public JsonDriver locate(Annotation[] fieldAnnotations, Optional driverName) {
        JsonDriverNotFound.failIf(ac == null, "null ApplicationContext in JsonType. This class is @Configurable. Use @EnableSpringConfigured or define an AnnotationBeanConfigurerAspect and @DependsOn('annotationBeanConfigurerAspect') on your datasource");
        final Map matchingBeans = ac.getBeansOfType(JsonDriver.class);
        JsonDriverNotFound.failIf(matchingBeans.isEmpty(), "no JsonDriver found in ApplicationContext");
        if (driverName.isPresent()) {
            JsonDriverNotFound.failIf(!matchingBeans.containsKey(driverName.get()), String.format("no JsonDriver named '%s' in ApplicationContext", driverName.get()));
            return matchingBeans.get(driverName.get());
        }
        JsonDriverNotFound.failIf(matchingBeans.size() > 1, "more than one JsonDriver found in ApplicationContext, use @JsonType.WithDriver to disambiguate");
        return matchingBeans.values().iterator().next();
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        this.ac = ac;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy