Spring:Grails的基礎

最近,Peter Ledbrook撰寫了一篇名爲《Spring:Grails的基礎》的文章,透過這些文字,咱們能夠了解Spring和Grails的關係,及其在Grails中發揮的做用。web

Grails自己也是一個Spring MVC應用,擁有本身的DispatcherServlet、ApplicationContext和相關的Bean,典型的Grails通常都有:spring

  • grailsApplication – 表明當前應用及其資源的Bean
  • pluginManager – 插件管理器,你能夠經過它查詢已加載插件的信息
  • jspViewResolver – 自定義的GSP MVC視圖解析器,在找不到相關GSP的狀況下會去找JSP
  • messageSource – 本地化的消息源
  • localeResolver – 肯定用戶的locale
  • annotationHandlerMapping – 容許使用@Controller註解

除了Web層,象其它如GORM、事務等,都離不開Spring的支持。正如Peter所言:app

所以,Grails應用實際就是Spring應用。jsp

在文章的第二部分,Peter着重解釋了Grails和Spring之間的交互。若是你仔細閱讀過Grails的參考文檔,那麼對於該部分的前兩項內容(服務和自動裝配,手動定義Bean)應該不會陌生。spa

Grails一樣也支持Spring的註解,但前提是要在grails-app/conf/Config.groovy裏進行配置,指定要掃描的包:.net

1 grails.spring.bean.packages = [ "org.example" ]

此外,Peter還給出了在Grails中得到WebApplicationContext的代碼示例:插件

1 import  org.springframework.web.context.support.WebApplicationContextUtils
2 import org.codehaus.groovy.grails.web.context.ServletContextHolder
3 import org.springframework.context.ApplicationContext
4 ...
5 def ctx = WebApplicationContextUtils
6  .getWebApplicationContext(ServletContextHolder.servletContext)

雖然簡單,但文章已經給讀者勾勒出了Grails和Spring的關係,以及它們之間的基本交互方式,請務必閱讀原文,瞭解其中的詳細內容。code

相關文章
相關標籤/搜索