• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Struts 2 , TypeConversion, Spring Injection

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've run across something slightly confusing to me in getting Dependency Injection with Spring to work with Type Converters under Struts 2.

I tried creating a StrutsTypeConverter subclass that had a @Transactional annotation with a @PersistenceContext on an EntityManager field. The Converter was declared as a bean in the applicationContext. However the EM was not injected.

I then made a wrapper class (Workspace) around an EntityManager (only exposes a get/set EntityManager), this wrapper class also had the @Transaction and @PersistenceContext annotations. The Workspace was declared in the application context.

I then made the converter expose a setWorkspace method. The converter was no longer listed in the ApplicationContext. However now the EM gets successfully injected into the Workspace; and the Workspace gets injected into the converter.

What is the difference, in these two approaches that causes one to work and one to not?

Is there something unique about the StrutsTypeConverter class that causes auto-wire by name works successfully (without even a bean definition of the converter), but not @Transactional/@PersistenceContext annotations?
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I got an answer to this on the struts users' mailing list. Here's the gist of it, in case anyone was wondering what was going on.

The Struts 2 ObjectFactory (normally backed by the Spring plugin, but other options are available) is responsible for creating all Actions, Converters, and Interceptors used by the framework. The default configuration of the struts-spring plugin will do a Spring autowire-by-name from the application context on the created objects. It does not, however have the functionality provided by PersistenceAnnotationBeanPostProcessor. Thus you can't use @PersistenceContexts annotations to inject EntityManagers into Actions, Converters, or Interceptors.
 
You get good luck from rubbing the belly of a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic