Home  >  Article  >  Java  >  How does SpringBoot delete useless beans in referenced jar packages?

How does SpringBoot delete useless beans in referenced jar packages?

王林
王林forward
2023-05-14 20:07:092130browse

Preface

The company has a project that is in a tight schedule, and some of the requirements of the project are the same as some of the functions of the previous project. In order to keep up with the speed, some modules in the previous multi-module maven project were directly transferred , directly introduced into the new project in the form of a jar package. Although it saves a lot of development time, it also causes the project to need to import the related dependencies of the project jar, causing the project to be bloated and slow to start. Is there a way to make the project load only the beans it needs?

Of course we can directly modify the source code and repackage and import it to solve the problem, but this method is too troublesome.

Through Baidu's method, the query can be implemented using the @ComponentScan annotation on the springboot startup class

Code example

@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
        pattern = {"com.xx.controller","com.xx.xx"})})

But after many implementations, it was found to be useless. The original project Beans outside the package are generally loaded into another project through the spring SPI spring.factories method.

spring.factories will create some beans defined in the jar, such as forcing formatted return values ​​

Later I found that by using BeanDefinitionRegistryPostProcessor, directly remove the bean registration information after parsing it. Just fine, so the bean won't be created.

BeanDefinitionRegistryPostProcessor inherits BeanFactoryPostProcessor to manage these beans

Implementation code example

Create a new RemoveRegistryBeanFactoryPostProcessor class in the project, the code is as follows

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;
 
/**
 * @author liuya
 */
@Component
public class RemoveRegistryBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
 
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        String[] names = registry.getBeanDefinitionNames();
        for (String name : names) {
            if (name.contains("taskSendMessageListener") || name.contains("globalListener") || name.contains("executionSendMessageListener") || name.contains("processCallbackMesController")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.system.flow")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.system.modules.message")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.graphics.task")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.graphics.bimlight.location")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.graphics.bimlight.sectioning")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.graphics.modules")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.ubw.job")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.ubw.listener")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.ubw.msg")) {
                registry.removeBeanDefinition(name);
            }
            if (name.contains("org.springblade.ubw.service")) {
                registry.removeBeanDefinition(name);
            }
        }
    }
 
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }
}

Of course there is some automatic configuration The class code needs to delete the bean registration. We can configure it through

@SpringBootApplication(exclude = {})的方式去实现,代码如下:
@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = {DllInitLoader.class,ProcessEngineServicesAutoConfiguration.class})
public class UnifyWorkFaceApplication {
    public static void main (String[] args) {
      BladeApplication.run("work-face", UnifyWorkFaceApplication.class, args);
    }
}

. The project startup speed is much faster, many jar dependencies are removed, and many useless tables are deleted, such as flowable workflow related tables. In the past, the relevant tables of the workflow were automatically queried when the project was started. As a result, the table project of the workflow that deleted the database would not be started. Now it is moved through

@SpringBootApplication(exclude = {ProcessEngineServicesAutoConfiguration.class}) In addition to the ProcessEngineServicesAutoConfiguration automatic configuration class code, there is also elimination and injection of project classes that reference flowable in the project, and it can start normally.

The above is the detailed content of How does SpringBoot delete useless beans in referenced jar packages?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete