This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

No transaction in transactional service called from @PostConstructPage Title Module

when using the PostInitialize in spring-enabled web environment, an error occurs where a call is made to:

Code:

Map beans = applicationContext.getBeansOfType(Object.class);

due to the existence of beans annotated with @Scope("request")

the error says that the bean cannot be created outside the context of a request. same behavior should be expected with session-bound beans. Therefore, the solution, although restricting the PostInitialize to singletons only, is change the line above to:

Comment

Comment

I came across this thread after battling with the same problem of trying to run a transaction in a @PostConstruct method. I managed to get a transaction to run if I annotated the Interface method of the service I am calling from within the PostConstruct method instead of the concrete class.

Jon

Comment

Probably it depends on your bean, because as you saw, there is no guarantee the annotations processing beans are already loaded when your @PostConstruct method is called. I still think you'd better still use the @PostInitialzer.

Comment

I don't know if you support your code from 2008, or maybe someone else has a better solution for spring 3.x ? I successfully used the @PostConstruct annotation but now, it doen't work anymore. I debugged the code and noticed, that the annotation finder iterates over the current class which is a Proxy instance and all superclasses (which is the Proxy superclass). So it will never ever see the actual annotation in the service class.

I thought about moving the @PostConstruct to the interface and change the Annotation scanner accordingly. But maybe someone else comes up with a much better Solution?

Comment

Yes I used it already in 3.0.x and it worked but iin the last days I tried to update to 3.0.1 and back to 3.0.0 and now I doesn't work any more. And I don't know why it worked before. It couldn't. I think spring beans have always been instances of Proxy, correct? So scanning the subclasses, only cannot work.

Comment

The instances aren't always proxies, it depends on their scope and the declared aspects (transaction annotations, custom AspectJ aspects, etc).
The scanning of interfaces is correct, but org.springframework.util.ClassUtils.getAllInterfac esForClass(Class<?> clazz) should be used to include ones implemented by superclasses.

Cheers,
Baruch.

Comment

The instances aren't always proxies, it depends on their scope and the declared aspects (transaction annotations, custom AspectJ aspects, etc).
The scanning of interfaces is correct, but org.springframework.util.ClassUtils.getAllInterfac esForClass(Class<?> clazz) should be used to include ones implemented by superclasses.

We seem to have a similar issue and although it was great to happen to come across the @PostInitialize solution it doesn't work for us... Spring 2.5.6-SEC01, GlassFish v3 Final, Java 1.6.0... .

The odd thing is that in an earlier more constrained example it seemed to work. This is the code we have and all beans are getting constructed fine its just that the initService is not getting called...

Comment

I decided to try the following code and clearly the @PostConstruct gets executed while the @PostInitialize does not... which at least implies that there are no issues in the code outside the @PostInitialize (for some reason).

However we can't use the @PostConstruct as the modalityDao bean is not fully available for executing a JPA call on it... hence why we REALLY need @PostInitialize... (very similar reasons to @Transactional not working).