OpenEntityManagerInViewFilter Problems

前端 未结 3 697
难免孤独
难免孤独 2020-12-16 07:30

I have scoured this site among others for answers in getting OpenEntityManagerInViewFilter to work. I have a standard User object that references a roles object with a many

3条回答
  •  无人及你
    2020-12-16 07:58

    But now!!

    Let me make a guess: the name of your application is: board?

    Correct? then go on and read the remaining answer!

    Yes you have two entity manager, and even two identical application contexts (one app context and one web context) -- So you have every bean twice!

    What happened is: you have only one (relevant) spring configuration file: 'board-servlet.xml' ('persistence-spring-beans.xml' is included in that file so at least it is one big logical file)

    And you create a context from this file twice in the 'web.xml':

    first:

    
        contextConfigLocation
        
            /WEB-INF/board-servlet.xml  *****This file references the file with entityManager declared*****
            /WEB-INF/board-security.xml
        
    
    ...
    
        org.springframework.web.context.ContextLoaderListener
    
    

    ContextLoaderListener load the application context specified by the files in 'contextConfigLocation' parameter.

    second:

    
        board
        org.springframework.web.servlet.DispatcherServlet
        1
    
    

    Dispatcher Servlet create the web application context, which xml file is:

    • named by the init-param 'contextConfigLocation'
    • or, if there is no such paramter, it looks for an file named '/WEB-INF/-servlet.xml'

    (For more details have a look at the Java Doc of FrameworkServlet)

    In your case there is no explicitly named file, so it reads the 'board-servlet.xml' again.

    What you need to do is separate them:

    • remove the from board-servlet.xml
    • change the contextConfigLocation in web.xml that it refers to classpath:persistence-spring-beans.xml and /WEB-INF/board-security.xml direcly
    • (not 100% necessary) separate the 'context:component-scan' so that a component scan in board-servlet.xml scan only for @Controller and the component scan in persistence-spring-beans.xml scan for the others (@Service, @Component, @Repository and @Dao)
    • last step: please tell me that it works now

提交回复
热议问题