为什么不上的Class.forName Grails领域类的工作(Why doesn't C

2019-08-01 05:12发布

I know that using Class.forName to load a grails domain class does not work, but I'm not sure why that is. I'm guessing there is some sort of grails magic happening but it would be nice to understand what it is.

I ended up using

GrailsDomainClass dc = grailsApplication.getDomainClass('mypack.myclass' )
def newDomainObject = dc.clazz.newInstance()

But I'm not sure why just doing Class.forName('mypack.myclass') doesn't work.

Answer 1:

Grails使用定制类装入器,所以你需要使用3-ARG变异与Grails使用并注册为的上下文ClassLoader类加载器:

Class clazz = Class.forName('mypack.myclass', true, Thread.currentThread().contextClassLoader)
def newDomainObject = clazz.newInstance()


文章来源: Why doesn't Class.forName work on grails domain classes