Spring Configuration

前端 未结 5 1237
情深已故
情深已故 2020-12-14 09:48

I\'ve been reading up on Spring and it keeps talking about the spring configuration data you need, but where do you put this xml file? and what do you save it as? I can\'t s

5条回答
  •  天涯浪人
    2020-12-14 10:21

    More interesting than the pure name is how do you split the files (and give each part a name).

    If you have a Standalone or Webapplication with out tests, then you can put all configuration in one file. - But having no test should not be a opinion.

    Lets assume you have a web application with tests.

    Then you should split the configuration in two files, one for the pure java (without the web suff) configuration and one that contains all the other stuff for the WEB application.

    I personaly perfer to name it applicationContext.xml and webmvc-config.xml. The default name for the web configuration file (if no specifed for the Dispatcher Servlet) would be /WEB-INF/-servlet.xml)

    I locate the applicationContext.xml in classpath:/META-INF/spring directory and the webmvc-config.xml in WEB-INF/spring. That location is the style of Spring Roo. It works, but every other folder would work too. Because I use maven the exact location of the files are:

    • /src/main/resources/META-INF/spring/applicationContext.xml
    • /src/main/webapp/WEB-INF/spring/webmvc-config.xml

    The core applicationContext.xml is loaded with the org.springframework.web.context.ContextLoaderListener, and the webmvc-config.xml by the Dispatacher Servlet. web.xml:

    
        contextConfigLocation
        classpath*:META-INF/spring/applicationContext*.xml
    
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        CFMA-SpringProject
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/spring/webmvc-config.xml
        
        1
    
    

    Now you start to write your tests, for the business logic of your application, without loading all the web stuff. But in most cases that is not enougth. For example you want to run some fast tests with an Inmemory Database while you run the normal Application with an persistent Database like MySql (please don't blame me for that sentence), or you want to use a jndi configured db in production and a "normal" configured one for tests. So what you need is two different configuration. To write not every thing twice, the easiest way is to split the applicationContext.xml in two files:

    • applicationContext.xml for the core stuff without the db stuff that differs to the tests
    • applicationContext-db.xml for the productive db configuration (for example jndi-lookup for db connection and LocalContainerEntityManagerFactoryBean for MySql)

    (Now you understand the pattern of contextConfigLocation in the web.xml)

    For the Tests you need now two files (you can write it in one file, but I prefer two). * testContext-h2DbConfig.xml The file that is the testing sibling of applicationContext-db.xml, but with the test database and without jndi. * textContext.xml This file in the one referenced by @ContextConfiguration in you test cases. This file contains only the imports of the configuration you need for the tests. In this case it is:

     
    
    

    Because I use spring, both files are located in /src/test/resources/META-INF/spring/testContext.xml

    If you have other aspects of your spring configuration, where the test and the productive configuration differs (for example schedulers), then you can split it in the same way.

    I hope you understand how splitting, naming convention and location work together.

提交回复
热议问题