resources.groovy grails文件中的springSecurityService?

时间:2012-03-27 09:03:57

标签: grails spring-security

我在从resources.groovy文件访问springSecurityService时遇到问题,我正在尝试加载用户区域设置并创建LocaleResolver

import User

beans = {
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
        def user = User.get(springSecurityService.principal.id)
        if (user?.settings?.locale) {
            defaultLocale = new Locale(user?.settings?.locale)
            java.util.Locale.setDefault(defaultLocale)
        }
    }
}

谢谢, 米卡

1 个答案:

答案 0 :(得分:2)

上面的代码没有多大意义。在resources.groovy中,您应该定义Spring bean的实现类并设置它们的依赖项。看起来您正试图在resources.groovy中实际编写实现类。

相反,您应该编写自己的LocaleResolver

package org.example

class MyLocaleResolver extends AbstractLocaleResolver {

  def springSecurityService

  // implementation of methods omitted, because I haven't clue how you want to resolve Locales
}

然后在resources.groovy中,定义一个替换默认localeResolver bean的此类bean

beans = {
    localeResolver(org.example.MyLocaleResolver) { 
        springSecurityService = ref('springSecurityService') 
    } 
}