0

I want to implement an DynamicImageResource which will be mounted in a Wicket 6.7/Spring 3.2/JPA 2 application. This DynamicImageResource shall retrieve information based on input parameters and create a response.

The question is: How can i access the DB from within DynamicImageResource.getImageData(...)? I can't inject a @PersistenceContextenter code here or a DAO via @SpringBean. My last resort would be a static PersistenceContextFactory.

Any better ideas? Or best practices?

lajuette
  • 997
  • 1
  • 7
  • 18

2 Answers2

4

As Wicket manages component instanciation for you, or you manually instanciate objects via their respective constructors, injection mechanisms couldn't work out of the box (the object are, per manual constructor invocation, unmanaged by your container).

Wicket provides built-in injection for Component via inheritance, the magic behind is Wicket calling componentInstanciationListeners. For every unmanaged class not inherited from an injection enabled one, you have to manually trigger the injection on your bean.

As stated in the comment, you should call Injector.get().inject(this); in your constructor in order for it to work.

Cedric Gatay
  • 1,553
  • 3
  • 12
  • 17
  • I doesn't seem to work in my project. In my WicketApp i added a SpringComponentInjector and in my Resource i'm calling .inject(this) as you suggestet. No luck... – lajuette Apr 22 '13 at 20:38
  • I don't work with Spring usually but with CDI / JavaEE injector, you can try to debug to see if the bean is really injected. – Cedric Gatay Apr 23 '13 at 06:48
  • It isn't. The reference is null. Strange. I'd like to use CDI, but i don't want to go "EE" for my tiny app. Maybe i should... – lajuette Apr 23 '13 at 14:09
  • 1
    You can have a look at the following question which is exactly what you've asked http://stackoverflow.com/questions/8155359/how-can-i-get-a-spring-bean-injected-in-my-custom-wicket-model-class – Cedric Gatay Apr 23 '13 at 14:16
  • ahhhh. stupid me. it works. Thank you. I just called the wrong Constructor. Not the default constructor containing Injector.get().inject(this). this() to the rescue! Still: I have to wrap my persistence context in a spring bean to inject it. – lajuette Apr 23 '13 at 14:29
1

To complete the whole thing, i'll past my PersistenceContextBridge Spring bean here. It wraps an EntityManager, which will be injected via @PersistenceContext. By using the Lombok annotation @Delegate all calls to the bean will be delegated to the EntityManager.

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import lombok.Delegate;
import org.springframework.stereotype.Service;

@Service
public class EntityManagerBridge implements EntityManager {
    @Delegate
    @PersistenceContext
    private EntityManager em;
}

Now all i have to do in my Wicket component is to inject EntityManagerBridge as @SpringBean:

@SpringBean
private EntityManager em; // inject EntityManagerBridge (implements EntityManager)

public MyFancyResource() {
    Injector.get().inject(this); // enable Spring injection for
}
lajuette
  • 997
  • 1
  • 7
  • 18