Last active
August 27, 2018 12:48
-
-
Save edpichler/9e22309a86b97dbd4cb1ffe011aa69dd to your computer and use it in GitHub Desktop.
A Spring utility class to return the application context. Works in server applications, standalone apps and jUnit tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebServlet(name = "MyWebHook", value = "/WebHook") | |
public class MyWebServlet extends HttpServlet { | |
private MyBean byBean | |
= SpringUtils.INSTANCE.ctx(this, true).getBean(MyBean.class); | |
public MyWebServlet() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company.web.spring | |
import com.company.jpa.spring.MyBusinessAppConfig | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.context.ApplicationContext | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext | |
import org.springframework.context.annotation.ComponentScan | |
import org.springframework.context.annotation.Configuration | |
import org.springframework.context.annotation.Import | |
import org.springframework.stereotype.Component | |
import org.springframework.web.context.ContextLoader | |
import org.springframework.web.context.WebApplicationContext | |
import org.springframework.web.context.support.WebApplicationContextUtils | |
import javax.servlet.http.HttpServlet | |
@Configuration | |
@Import(value = [MyBusinessAppConfig::class]) | |
@ComponentScan(basePackageClasses = [SpringUtils::class]) | |
open class WebAppConfig { | |
} | |
/** | |
* | |
* Singleton object to create (only if necessary), return and reuse a Spring Application Context. | |
* | |
* When you instantiates a class by yourself, spring context does not autowire its properties, but you can wire by yourself. | |
* This class helps to find a context or create a new one, so you can wire properties inside objects that are not | |
* created by Spring (e.g.: Servlets, usually created by the web server). | |
* | |
* Sometimes a SpringContext is created inside jUnit tests, or in the application server, or just manually. Independent | |
* where it was created, I recommend you to configure your spring configuration to scan this SpringUtils package, so the 'springAppContext' | |
* property will be used and autowired at the SpringUtils object the start of your spring context, and you will have just one instance of spring context public available. | |
* | |
*Ps: Even if your spring configuration doesn't include the SpringUtils @Component, it will works tto, but it will create a second Spring Context o your application. | |
*/ | |
@Component | |
object SpringUtils { | |
var springAppContext: ApplicationContext? = null | |
@Autowired | |
set(value) { | |
field = value | |
} | |
/** | |
* Tries to find and reuse the Application Spring Context. If none found, creates one and save for reuse. | |
* @return returns a Spring Context. | |
*/ | |
fun ctx(): ApplicationContext { | |
if (springAppContext!= null) { | |
println("achou") | |
return springAppContext as ApplicationContext; | |
} | |
//springcontext not autowired. Trying to find on the thread... | |
val webContext = ContextLoader.getCurrentWebApplicationContext() | |
if (webContext != null) { | |
springAppContext = webContext; | |
println("achou no servidor") | |
return springAppContext as WebApplicationContext; | |
} | |
println("nao achou, vai criar") | |
//None spring context found. Start creating a new one... | |
val applicationContext = AnnotationConfigApplicationContext ( WebAppConfig::class.java ) | |
//saving the context for reusing next time | |
springAppContext = applicationContext | |
return applicationContext | |
} | |
/** | |
* @return a Spring context of the WebApplication. | |
* @param createNewWhenNotFound when true, creates a new Spring Context to return, when no one found in the ServletContext. | |
* @param httpServlet the @WebServlet. | |
*/ | |
fun ctx(httpServlet: HttpServlet, createNewWhenNotFound: Boolean): ApplicationContext { | |
try { | |
val webContext = WebApplicationContextUtils.findWebApplicationContext(httpServlet.servletContext) | |
if (webContext != null) { | |
return webContext | |
} | |
if (createNewWhenNotFound) { | |
//creates a new one | |
return ctx() | |
} else { | |
throw NullPointerException("Cannot found a Spring Application Context."); | |
} | |
}catch (er: IllegalStateException){ | |
if (createNewWhenNotFound) { | |
//creates a new one | |
return ctx() | |
} | |
throw er; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment