I started recently a project with the Spring framework with the ojective to develop it without none XML config file, only Java code.
In this current moment, I add th
Maybe there're some conflicting overlapping between your web.xml
and java-based configuration class
. That's the root cause when I met the same error as you. And my solution is to comment out the duplicated configuration in web.xml
.
I had a class
MyContextLoaderListener extends ContextLoaderListener
somewhere in my code. It was unused. Spring boot apparently used reflection to find it. I deleted the file and the problem was gone
java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
In your WebAppInitializer
, you are registering a ContextLoaderListener
.
Because you are using AbstractSecurityWebApplicationInitializer
with the super
constructor that accepts a Class[]
Creates a new instance that will instantiate the ContextLoaderListener with the specified classes.
that also registers a ContextLoaderListener
. Note the class javadoc
When used with
AbstractSecurityWebApplicationInitializer(Class...)
, it will also register aContextLoaderListener
. When used withAbstractSecurityWebApplicationInitializer()
, this class is typically used in addition to a subclass ofAbstractContextLoaderInitializer
.
As the error states, you cannot have two ContextLoaderListener
instances because they will both try to create and add a ApplicationContext
to the ServletContext
.
I think the problem is that this:
.loginPage("spring/index").permitAll()
should be this:
.loginPage("/spring/index").permitAll() //note the '/' at the beginning of the uri