gpc / rendering

Provides rendering of GSPs as PDFs, JPEGs, GIFs and PNGs
http://gpc.github.com/rendering
Apache License 2.0
31 stars 45 forks source link

No wiring beans when using Grails Rendering Plugin #31

Closed magx2 closed 8 years ago

magx2 commented 8 years ago

When I add Grails Rendering Plugin (compile "org.grails.plugins:rendering:2.0.1") into my dependencies all my SpockUnit test with controllers are getting broken.

The error that I got is:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [grails.plugins.rendering.image.PngRenderingService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 38 more

Also I have in my build.gradle file dependency on Spring test: runtime 'org.springframework:spring-test:4.1.7.RELEASE'. As far as I know this is needed for rendering plugin

Grails version: 3.0.9

yvesf commented 8 years ago

Try to activate the services and rending plugin for the test by declaring this the test class:

import static grails.test.runtime.GrailsApplicationTestPlugin.TestRuntimeGrailsApplicationPostProcessor.DEFAULT_INCLUDED_PLUGINS

...
  static includePlugins = DEFAULT_INCLUDED_PLUGINS + ['services', 'rendering']

I would be very much in favor of removing the automatic injection of the pdfRenderingService by the trait.

graemerocher commented 8 years ago

The solution is to make each wired bean use required=false:

https://github.com/gpc/rendering/blob/master/src/main/groovy/grails/plugins/rendering/RenderingTrait.groovy#L32

Example:

     @Autowired(required=false)

and then add a null check in the render methods.

Pull requests welcome.

magx2 commented 8 years ago

@graemerocher What kind of null check you want?

smt like this:

if (pdfRenderingService) {
    // do stuff
} else {
    log.warning(...)
}

or

if (pdfRenderingService) {
    // do stuff
} else {
   throw new IllegalStateException('there is no pdfRenderingBean')
}
graemerocher commented 8 years ago

the latter seems reasonable

magx2 commented 8 years ago

Please look at PR #32

graemerocher commented 8 years ago

merged thanks

magx2 commented 8 years ago

Will you do release with this issue?