Diferencia entre revisiones de «Configuracion De Spring»
(Página nueva: Describe ConfiguracionDeSpring here. ==Ver también== * Obtener Variables De Entorno Con Spring) |
(→Ver también) |
||
| (No se muestran 14 ediciones intermedias de 5 usuarios) | |||
| Línea 1: | Línea 1: | ||
| − | + | [[Category:Spring Framework]] | |
| + | 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. | ||
| + | |||
| + | {{aviso|texto=La clase ContextLoaderServlet queda obsoleta a partir de Spring Framework 3.0}} | ||
| + | |||
| + | ===Configuración usando ContextLoaderListener=== | ||
| + | <code xml> | ||
| + | <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> | ||
| + | </code> | ||
| + | |||
| + | ===Obtención del contexto=== | ||
| + | Con el contexto configurado, se utiliza la clase ''WebApplicationContextUtils'' para acceder al contexto de Spring. | ||
| + | <code java> | ||
| + | ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); | ||
| + | FooBean foo = (FooBean) context.getBean("fooBean"); | ||
| + | </code> | ||
==Ver también== | ==Ver también== | ||
* [[Obtener Variables De Entorno Con Spring]] | * [[Obtener Variables De Entorno Con Spring]] | ||
| + | * [[PropertyPlaceholderConfigurer]] | ||
| + | * [[Buenas Practicas De Configuracion De Spring]] | ||
| + | * [[Compartir contexto de Spring entre aplicaciones web]] | ||
| + | * [http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html Javadoc de WebApplicationContextUtils] | ||
Revisión actual del 12:53 21 ene 2011
Dependiendo el tipo de aplicación y entorno, la configuración del contexto de Spring Framework varia.
Contenido
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");