Diferencia entre revisiones de «Configuracion De Spring»

De Dos Ideas.
Saltar a: navegación, buscar
(Aplicaciones web)
(Ver también)
Línea 35: Línea 35:
 
* [[Obtener Variables De Entorno Con Spring]]
 
* [[Obtener Variables De Entorno Con Spring]]
 
* [http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html Javadoc de WebApplicationContextUtils]
 
* [http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html Javadoc de WebApplicationContextUtils]
 +
* [[Buenas Practicas De Configuracion Con Spring]]

Revisión del 11:19 25 feb 2009

Dependiendo el tipo de aplicación y entorno, la configuración del contexto de Spring Framework varia.

Aplicaciones web

En las aplicaciones web, Spring puede configurarse de manera muy simple a través de un Listener o un Servlet.

En el archivo web.xml se agrega la variable de contexto contextConfigLocation, la cual apunta a los archivos de configuración. Luego, puede utilizarse el listener ContextLoaderListener o el servlet ContextLoaderServlet (ambos provistos por el framework) para que inicialicen el contexto de Spring.

Configuración usando ContextLoaderListener

<web-app>

   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
           classpath:archivoDeSpring1.xml,
           classpath:archivoDeSpring2.xml
       </param-value>
   </context-param>

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

</web-app>

Obtención del contexto

Con el contexto configurado, se utiliza la clase WebApplicationContextUtils para acceder al contexto de Spring. ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); FooBean foo = (FooBean) context.getBean("fooBean");

Ver también