search
HomeJavajavaTutorialHow does SpringBoot delete useless beans in referenced jar packages?

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:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor