我的Grails应用程序之一中有一个服务类,如下所示:
class CertificateArtService {
GrailsApplication grailsApplication
@Lazy
File extraBoldFontFile = grailsApplication.mainContext.getResource("classpath:resources/OPENSANS-EXTRABOLD.TTF").file
@Lazy
File semiBoldFontFile = grailsApplication.mainContext.getResource("classpath:resources/OPENSANS-SEMIBOLD.TTF").file
@Lazy
Font titleFullNameFont = Font.createFont(Font.TRUETYPE_FONT, extraBoldFontFile).deriveFont(80f)
@Lazy
Font certificateTitleFont = Font.createFont(Font.TRUETYPE_FONT, semiBoldFontFile).deriveFont(60f)
@Lazy
Color titleFullNameColor = new Color(19, 148, 215)
...
}
我曾经在函数内部创建了所有这些对象,并且一切工作都很好,但是当我决定进行一些代码清理并重用其中的大多数对象时,我开始遇到以下错误:
Cannot get property 'mainContext' on null object. Stacktrace follows:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'priz.certificate.CertificateArtController': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'certificateArtService': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [priz.certificate.CertificateArtService]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot get property 'mainContext' on null object
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:73)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at grails.plugin.springsecurity.rest.RestLogoutFilter.doFilter(RestLogoutFilter.groovy:82)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'certificateArtService': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [priz.certificate.CertificateArtService]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot get property 'mainContext' on null object
我想念什么?
更新
因此,按照建议,我将实现更新为:
@Value("classpath:resources/OPENSANS-EXTRABOLD.TTF")
private Resource extraBoldFontFileResource
@Value("classpath:resources/OPENSANS-SEMIBOLD.TTF")
private Resource semiBoldFontFileResource
@Lazy
Font titleFullNameFont = Font.createFont(Font.TRUETYPE_FONT, extraBoldFontFileResource.file).deriveFont(80f)
@Lazy
Font certificateTitleFont = Font.createFont(Font.TRUETYPE_FONT, semiBoldFontFileResource.file).deriveFont(60f)
资源正在正确初始化,我可以在类函数中的任何地方使用它们。但是,在最后两行中,我无法执行该操作,此时的资源似乎没有初始化,并且仍然为空。