Why this Spring application with java-based configuration don't work properly

前端 未结 4 1517
忘了有多久
忘了有多久 2020-12-01 14:48

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

相关标签:
4条回答
  • 2020-12-01 15:03

    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.

    0 讨论(0)
  • 2020-12-01 15:05

    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

    0 讨论(0)
  • 2020-12-01 15:12
    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 a ContextLoaderListener. When used with AbstractSecurityWebApplicationInitializer(), this class is typically used in addition to a subclass of AbstractContextLoaderInitializer.

    As the error states, you cannot have two ContextLoaderListener instances because they will both try to create and add a ApplicationContext to the ServletContext.

    0 讨论(0)
  • 2020-12-01 15:21

    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
    
    0 讨论(0)
提交回复
热议问题