Не удается получить свойство 'mainContext' для нулевого объекта - PullRequest
0 голосов
/ 21 декабря 2018

У меня есть класс обслуживания в одном из наших приложений 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)

Ресурсы инициализируются должным образом, и я могу использовать их где угодно в функциях класса.Однако в последних двух строках я не могу, ресурс в этой точке, кажется, не инициализирован и все еще нулевой.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...