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

com.yomahub.liteflow.spi.spring.SpringAware Maven / Gradle / Ivy

The newest version!
package com.yomahub.liteflow.spi.spring;

import cn.hutool.core.util.ObjectUtil;
import com.yomahub.liteflow.core.proxy.DeclWarpBean;
import com.yomahub.liteflow.spi.ContextAware;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Map;

/**
 * 基于代码形式的spring上下文工具类
 *
 * @author Bryan.Zhang
 */
public class SpringAware implements ApplicationContextAware, ContextAware {

    private static ApplicationContext applicationContext = null;

    public SpringAware() {
    }

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

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public  T getBean(String name) {
        T t = (T) applicationContext.getBean(name);
        return t;
    }

    @Override
    public  Map getBeansOfType(Class type) {
        return applicationContext.getBeansOfType(type);
    }

    @Override
    public  T getBean(Class clazz) {
        T t = applicationContext.getBean(clazz);
        return t;
    }

    private  T getBean(String beanName, Class clazz) {
        T t = applicationContext.getBean(beanName, clazz);
        return t;
    }

    @Override
    public  T registerBean(String beanName, Class c) {
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
                .getAutowireCapableBeanFactory();
        BeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClassName(c.getName());
        beanFactory.setAllowBeanDefinitionOverriding(true);
        beanFactory.registerBeanDefinition(beanName, beanDefinition);
        return getBean(beanName);
    }

    @Override
    public Object registerDeclWrapBean(String beanName, DeclWarpBean declWarpBean) {
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
                .getAutowireCapableBeanFactory();
        beanFactory.setAllowBeanDefinitionOverriding(true);

        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(DeclWarpBean.class);
        beanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);
        MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
        mutablePropertyValues.add("nodeId", declWarpBean.getNodeId());
        mutablePropertyValues.add("nodeName", declWarpBean.getNodeName());
        mutablePropertyValues.add("nodeType", declWarpBean.getNodeType());
        mutablePropertyValues.add("rawClazz", declWarpBean.getRawClazz());
        mutablePropertyValues.add("methodWrapBeanList", declWarpBean.getMethodWrapBeanList());
        mutablePropertyValues.add("rawBean", declWarpBean.getRawBean());
        beanDefinition.setPropertyValues(mutablePropertyValues);

        beanFactory.registerBeanDefinition(beanName, beanDefinition);
        return getBean(beanName);
    }

    @Override
    public  T registerBean(Class c) {
        return registerBean(c.getName(), c);
    }

    @Override
    public  T registerBean(String beanName, Object bean) {
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext
                .getAutowireCapableBeanFactory();
        defaultListableBeanFactory.registerSingleton(beanName, bean);
        return (T) configurableApplicationContext.getBean(beanName);
    }

    @Override
    public  T registerOrGet(String beanName, Class clazz) {
        if (ObjectUtil.isNull(applicationContext)) {
            return null;
        }
        try {
            return getBean(beanName, clazz);
        } catch (Exception e) {
            return registerBean(beanName, clazz);
        }
    }

    @Override
    public boolean hasBean(String beanName) {
        return applicationContext.containsBean(beanName);
    }

    @Override
    public int priority() {
        return 1;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy