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

yakworks.gorm.boot.DefaultCrudApiConfiguration.groovy Maven / Gradle / Ivy

The newest version!
/*
* Copyright 2024 Yak.Works - Licensed under the Apache License, Version 2.0 (the "License")
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
package yakworks.gorm.boot

import java.util.function.Function

import groovy.transform.CompileStatic

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Scope
import org.springframework.core.ResolvableType

import yakworks.gorm.api.CrudApi
import yakworks.gorm.api.DefaultCrudApi

@CompileStatic
@Configuration
class DefaultCrudApiConfiguration {

    @Autowired
    private ApplicationContext appCtx;

    /**
     * Factory function to return bean if one exists or create one with the defaultCrudApi prototype method
     *
     * 
{@code
     * // example usage
     *
     * CrudApi crudApi
     * @Autowired Function crudApiFactory
     *
     * CrudApi getCrudApi(){
     *   if (!crudApi) {
     *     this.crudApi = crudApiFactory.apply(getEntityClass())
     *   }
     *   return crudApi
     * }
     *
     * }
* */ @Bean public Function, CrudApi> crudApiFactory() { return (Class clazz) -> { var rt = ResolvableType.forClassWithGenerics(CrudApi.class, clazz); return (CrudApi) appCtx.getBeanProvider(rt).getIfAvailable( () -> defaultCrudApi(clazz) ); } as Function, CrudApi> } @Bean @Scope("prototype") public DefaultCrudApi defaultCrudApi(Class entityClass) { return new DefaultCrudApi(entityClass); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy