Get the App
SLTechnology News&Howtos  ›  Development  › 

What if @ Async in Spring cannot implement async?

Shulou Source: shulou.com Published: 2022-06-03 07:55:41 11月15日 Update

This article will explain in detail what to do if @ Async cannot be asynchronized in Spring. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Spring @ Async cannot implement the cause of asynchronous problem

There are two configuration files in the project: springMVC.xml and beanDefines.xml, both of which are configured.

It is also configured in beanDefines.xml.

From the startup log, it is found that spring loads beanDefines.xml first and then springMVC.xml.

Because the context:component-scan of springMVC.xml overrides the configuration of beanDefines.xml, whether it is effective depends on the configuration of context:component-scan.

Causes the beanDefines.xml configuration not to work.

Solution.

1. Delete the sum of beanDefines.xml

2. Add configuration to springMVC.xml.

@ Async does not work

Let's first introduce how to @ Async annotations.

The 1.xml header file must be configured with the marked red part

two。 Configure task and thread pool 3. Configure the jar that spring depends on (not to cover this) 4. Use @ Async annotations on methods that you need to call asynchronously

5. But you may encounter a time when @ Async doesn't work.

My problem is that the container startup loads the following two xml files

The springmvc.xml file only configures scan annotations, while applicationContext.xml configures tasks and thread pools and scan annotations, loading applicationContext.xml first and then springMvc.xml.

ApplicationContext.xml adds @ Async to the scanned class (the configuration of the task and thread pool depends on the scanned class), and then the bean loaded by springMvc overrides applicationContext's bean, overriding the role of @ Async, because springmvc does not configure task and thread pool

As a result, @ Async does not work.

This is the end of the article on "what if @ Async cannot be asynchronous in Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Configuration function task file comment thread article how to do more problem add good practical effective two content method reason container article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information MariaDB Shulou Technology Redmi