Details
-
Type:
Bug
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: 1.0.2
-
Fix Version/s: None
-
Component/s: Services
-
Labels:None
-
Environment:Java 1.6
Windows XP
Description
I'm annotating some of my service methods using @javax.annotation.PostConstruct. This ensures spring will call that method on the bean after the bean is created. To get this working, I added " bean(org.springframework.context.annotation.CommonAnnotationBeanPostProcessor)" to resources.groovy.
This is failing silently if either the service has "static transactional = true" or if the service is injected into another service that has "static transactional = true".
See the attached sample grails project created with "grails bug-report". If you run "grails run-app", you will see the message on stdout: "setUp() annotated with @PostConstruct called in PostConstructService". If you change the transactional property to false in services/PostConstructService.groovy or services/UsesPostConstructService.groovy, then the @PostConstruct method will not be called and the message above will not be printed to stdout.
A workaround for this is to implement Spring's InitializingBean interface and define afterPropertiesSet(). This is a little more invasive than using the @PostConstruct annotation.
Thanks,
Jon
with the following configuration, I could use @PostConstruct for services in bean creation without problem:
The above configuration essentially add AutowiredAnnotationBeanPostProcessor on top of the CommonAnnotationBeanPostProcessor, that support fine-grained control. So, i think if configured correctly, @PostConstruct works fine in the latest stable version of Grails. (Grails 1.0.3+)
Reference: http://www.infoq.com/articles/spring-2.5-part-1
however, i suspect, in development mode, when the bean is changed, the @PostConstruct method is not called again. The service reload issue is complex and it may or may not related to @PostConstruct. If possible, please verify the development mode reload behavior.