Component of Tomcat+Spring strange event starts twice

I don't know what's going on, but when I started tomcat, I found a strange phenomenon. A Component used to load the global configuration of resources was loaded twice when tomcat started:

The specific situation is this


I created an Env Component, hoping that the project will load some global configurations at runtime, such as the development and test environment distinction, and the initialization of some resources, but at startup, I found the following problems through the log


The Env class is initialized twice, why?

At the beginning, I searched for the answer on the Internet with questions, saying that it was a problem with the configuration of server.xml under tomcat, and then I changed it to the following configuration


According to the answers given on the Internet, by adding deployIgnore, changing appBase, etc., I tried every method, but it still didn't work. At this moment, I was wondering if it was a problem with my spring configuration, so I finally found the truth.


There are two configuration spring files here, app-root corresponds to applicationContext.xml, and app-servlet corresponds to spring-mvc

Let's take a look at our wrong spelling first:

In app-root we write like this


In app-servlet, we write like this


ok, after looking for a mess of information on the Internet, I confirmed that the correct spelling is as follows:

It should be written like this in app-servlet:


First, add use-default-filters="false" to cancel the use of the default filter rules

Second, because we filter out the loading of the controller in app-root, we need to add the processing of the controller here

The problem has been solved here, let's analyze the cause of the problem:

In app-root, we filter out the loading of the controller, but do not limit the loading of Server, Component, and Repository, so when the program is executed to the app-servlet, because use-default-filters defaults to true, that is, there is no filtering rule, The Server, Component, and Repository will be loaded repeatedly. At this point, we have found the root cause of the problem.

First, filter the blacklist through exclude-filter;

Then whitelist filtering through include-filter;

Set use-default-filters to false, we set the filtering rules by ourselves, which avoids repeated loading


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325465251&siteId=291194637