diff --git a/plugin/build.gradle b/plugin/build.gradle index ce913e1..32fbad9 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -20,6 +20,9 @@ plugins { // Apply the Groovy plugin to add support for Groovy id 'groovy' + + // Apply the Spring dependency management plugin for managing dependencies + id 'io.spring.dependency-management' version '1.1.4' } apply plugin: 'java' @@ -160,4 +163,13 @@ dependencies { implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.9.0' // The "validation-api" library, version 2.0.1.Final, provides tools for validating Java objects according to defined constraints. implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' + // The "spring-core" library, version 5.3.31, is a fundamental component of the Spring Framework, + // offering essential functionality for dependency injection, bean management, and core utilities to facilitate robust Java application development within the Spring ecosystem. + implementation group: 'org.springframework', name: 'spring-core', version: '5.3.31' + // The "spring-boot-starter-web" library, version 2.7.18, is a Spring Boot starter module that facilitates the setup of web applications, + // providing essential dependencies and configurations for building web-based projects. + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.7.18' + // The "spring-boot-configuration-processor" library, version 2.7.18, + // is a Spring Boot module that processes configuration metadata annotations to generate metadata files and aid in auto-configuration of Spring applications. + implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '2.7.18' } diff --git a/plugin/src/main/groovy/org/redis4j/config/Redis4jBeanConfig.java b/plugin/src/main/groovy/org/redis4j/config/Redis4jBeanConfig.java new file mode 100644 index 0000000..89c455d --- /dev/null +++ b/plugin/src/main/groovy/org/redis4j/config/Redis4jBeanConfig.java @@ -0,0 +1,99 @@ +package org.redis4j.config; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.stereotype.Component; + +/** + * Configures access to the Spring application context. + */ +@Component +public class Redis4jBeanConfig implements BeanFactoryPostProcessor { + + private static ConfigurableListableBeanFactory beanFactory; + + /** + * Retrieves the ConfigurableListableBeanFactory instance. + * + * @return the ConfigurableListableBeanFactory instance + */ + public static ConfigurableListableBeanFactory getBeanFactory() { + return beanFactory; + } + + /** + * Retrieves a bean by name from the Spring application context. + * + * @param name the name of the bean to retrieve + * @param the type of the bean + * @return the bean instance + * @throws BeansException if an error occurs while retrieving the bean + */ + @SuppressWarnings({"unchecked"}) + public static T getBean(String name) throws BeansException { + return (T) getBeanFactory().getBean(name); + } + + /** + * Retrieves a bean by type from the Spring application context. + * + * @param clazz the class of the bean to retrieve + * @param the type of the bean + * @return the bean instance + * @throws BeansException if an error occurs while retrieving the bean + */ + public static T getBean(Class clazz) throws BeansException { + return getBeanFactory().getBean(clazz); + } + + /** + * Checks if a bean with the specified name exists in the Spring application context. + * + * @param name the name of the bean to check + * @return true if the bean exists, false otherwise + */ + public static boolean containsBean(String name) { + return getBeanFactory().containsBean(name); + } + + /** + * Checks if the bean with the specified name is a singleton in the Spring application context. + * + * @param name the name of the bean to check + * @return true if the bean is a singleton, false otherwise + * @throws NoSuchBeanDefinitionException if the bean definition cannot be found + */ + public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { + return getBeanFactory().isSingleton(name); + } + + /** + * Retrieves the type of the bean with the specified name from the Spring application context. + * + * @param name the name of the bean + * @return the type of the bean + * @throws NoSuchBeanDefinitionException if the bean definition cannot be found + */ + public static Class getType(String name) throws NoSuchBeanDefinitionException { + return getBeanFactory().getType(name); + } + + /** + * Retrieves the aliases for the bean with the specified name from the Spring application context. + * + * @param name the name of the bean + * @return an array of alias names + * @throws NoSuchBeanDefinitionException if the bean definition cannot be found + */ + public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { + return getBeanFactory().getAliases(name); + } + + @SuppressWarnings({"NullableProblems"}) + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { + Redis4jBeanConfig.beanFactory = factory; + } +} \ No newline at end of file