Vra

Wat is die korrekte manier om Spring se konfigurasie na veelvuldige xml-lêers te verdeel?

Op die oomblik het ek

  • /WEB-INF/foo-servlet.xml
  • /WEB-INF/foo-service.xml
  • /WEB-INF/foo-persistence.xml

My web.xml het die volgende:

<servlet>
    <description>Spring MVC Dispatcher Servlet</description>
    <servlet-name>intrafest</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/foo-*.xml
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/foo-*.xml
    </param-value>
</context-param>


<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

Die werklike vrae:

  • Is hierdie benadering korrek/beste?
  • Moet ek regtig die konfigurasie-liggings in beide in die DispatcherServlet EN die context-param afdelings?

Wat moet ek in gedagte hou om te kan verwys na bone gedefinieer in foo-servlet.xml van foo-service.xml?Het dit iets te doen met spesifikasie contextConfigLocation in web.xml?

Opdatering 1:

Ek gebruik Lente raamwerk 3.0.Dit is my begrip dat ek nie hulpbroninvoer soos volg hoef te doen nie:

 <import resource="foo-services.xml"/> 

Is dit 'n korrekte aanname?

Was dit nuttig?

Oplossing

Ek vind die volgende opstelling die maklikste.

Gebruik die verstek konfigurasie lêer laai meganisme van DispatcherServlet:

Die raamwerk sal, na inisialisering van 'n Dispatcherservlet, kyk na 'n lêer met die naam [Servlet-Name] -Servlet.xml in die web-Inf-gids van u webtoepassing en skep die boontjies wat daar gedefinieër is (ignoreer die definisies van enige boontjies wat met die gedefinieer is met die gedefinieerde met die dieselfde naam in die wêreldwye omvang).

In jou geval, skep eenvoudig 'n lêer intrafest-servlet.xml in die WEB-INF dir en hoef nie enige spesifieke inligting in te spesifiseer nie web.xml.

In intrafest-servlet.xml lêer wat jy kan gebruik invoer om jou XML-konfigurasie saam te stel.

<beans>
  <bean id="bean1" class="..."/>
  <bean id="bean2" class="..."/>

  <import resource="foo-services.xml"/>
  <import resource="foo-persistence.xml"/>
</beans>

Let daarop dat die Spring-span eintlik verkies om veelvuldige konfigurasielêers te laai wanneer die (Web)ApplicationContext geskep word.As jy dit steeds op hierdie manier wil doen, dink ek jy hoef nie albei konteksparameters te spesifiseer nie (context-param) en servlet inisialisering parameters (init-param).Een van die twee sal doen.Jy kan ook kommas gebruik om veelvuldige konfigurasieliggings te spesifiseer.

Ander wenke

Mike Nereson het dit te sê op sy blog by:

http://blog.codehangover.com/load-multiple-contexts-into-spring/

Daar is 'n paar maniere om dit te doen.

1.web.xml contextConfigLocation

U eerste opsie is om hulle almal in u webtoepassing te laai konteks via die ContextConfigLocation-element.Jy gaan reeds om jou primêre toepassing te hêKonteks hier, met die veronderstelling dat jy skryf 'n webtoepassing.Al wat jy hoef te doen is om 'n bietjie wit spasie tussen te sit die verklaring van die volgende konteks.

  <context-param>
      <param-name> contextConfigLocation </param-name>
      <param-value>
          applicationContext1.xml
          applicationContext2.xml
      </param-value>
  </context-param>

  <listener>
      <listener-class>
          org.springframework.web.context.ContextLoaderListener
      </listener-class>
  </listener>

Bogenoemde maak gebruik van koetsretoer.Alternatiewelik kan u net 'n ruimte inbring.

  <context-param>
      <param-name> contextConfigLocation </param-name>
      <param-value> applicationContext1.xml applicationContext2.xml </param-value>
  </context-param>

  <listener>
      <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
  </listener>

2.applicationContext.xml invoerhulpbron

U ander opsie is om net u primêre toepassingscontext.xml by die web.xml te voeg en dan invoerstate in daardie primêre konteks te gebruik.

In applicationContext.xml jy het dalk...

  <!-- hibernate configuration and mappings -->
  <import resource="applicationContext-hibernate.xml"/>

  <!-- ldap -->
  <import resource="applicationContext-ldap.xml"/>

  <!-- aspects -->
  <import resource="applicationContext-aspects.xml"/>

Watter strategie moet jy gebruik?

1. Ek verkies altyd om op te laai via web.xml.

Want , dit stel my in staat om alle kontekste van elkeen geïsoleer te hou Ander.Met toetse kan ons net die kontekste laai wat ons moet uitvoer daardie toetse.Dit maak ontwikkeling ook meer modulêr as komponente Bly loosely coupled, sodat ek in die toekoms 'n pakket kan onttrek of vertikale laag en skuif dit na sy eie module.

2. As jy kontekste laai in 'n non-web application, Ek sou die gebruik import hulpbron.

Daar is twee tipes kontekste waarmee ons te doen het:

1:wortelkonteks (ouerkonteks.Sluit tipies alle jdbc (ORM, Hibernate) inisialisering en ander veer sekuriteit verwante konfigurasie in)

2:individuele servlet-konteks (kind-konteks. Tipies Dispatcher Servlet-konteks en inisialiseer alle bone wat verband hou met spring-mvc (beheerders, URL-kartering, ens.)).

Hier is 'n voorbeeld van web.xml wat verskeie toepassingskontekslêer insluit

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Spring Web Application example</display-name>

    <!-- Configurations for the root application context (parent context) -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context -->
            /WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context -->
        </param-value>
    </context-param>

    <!-- Configurations for the DispatcherServlet application context (child context) -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/mvc/spring-mvc-servlet.xml
            </param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/admin/*</url-pattern>
    </servlet-mapping>

</web-app>

@eljenso:intrafest-servlet.xml webtoepassing konteks xml sal gebruik word as die toepassing SPRING WEB MVC gebruik.

Andersins is die @kosoant-konfigurasie goed.

Eenvoudige voorbeeld as jy nie SPRING WEB MVC gebruik nie, maar SPRING IOC wil gebruik:

In web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
</context-param>

Dan sal jou application-context.xml bevat: <import resource="foo-services.xml"/>hierdie invoerstellings om verskeie toepassingskontekslêers te laai en in hooftoepassing-context.xml te plaas.

Dankie en hoop dit help.

Ek is die skrywer van modulêre-lente-kontekste.

Dit is 'n klein nutsbiblioteek om 'n meer modulêre organisasie van lentekontekste moontlik te maak as wat bereik word deur gebruik te maak Stel XML-gebaseerde konfigurasie-metadata saam. modular-spring-contexts werk deur modules te definieer, wat basies alleenstaande toepassingskontekste is en modules toe te laat om boontjies van ander modules in te voer, wat in hul oorspronklike module uitgevoer word.

Die sleutelpunte is dan

  • beheer oor afhanklikhede tussen modules
  • beheer oor watter bone uitgevoer word en waar dit gebruik word
  • verminderde moontlikheid om botsings van bone te noem

'n Eenvoudige voorbeeld sal soos volg lyk:

lêer moduleDefinitions.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />

    <module:module id="serverModule">
        <module:config location="/serverModule.xml" />
    </module:module>

    <module:module id="clientModule">
        <module:config location="/clientModule.xml" />
        <module:requires module="serverModule" />
    </module:module>

</beans>

lêer serverModule.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />

    <bean id="serverSingleton" class="java.math.BigDecimal" scope="singleton">
        <constructor-arg index="0" value="123.45" />
        <meta key="exported" value="true"/>
    </bean>

</beans>

lêer clientModule.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />

    <module:import id="importedSingleton" sourceModule="serverModule" sourceBean="serverSingleton" />

</beans>
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top