1

Dozer's documentation states that you should only have one instance of DozerBeanMapper running in the app on the server. For initial development I ignored this, now I want to update the app to do this.

How can I instantiate the DozerBeanMapper class when the application starts on glassfish, and how would I access its "map" method in another class once the application has started or been newly deployed?

This is for EJBs so I can't use any servlet to do this.



OK, so I've finally had time to refactor this code. With the pointer from @Mikko Maunu, I am editing my question to provide the code that I have working for me for anyone who might find it useful in the future.

package com.xyz.utilities.singleton;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.dozer.DozerBeanMapper;

@Startup
@Singleton
public class DozerInstantiator {

  private DozerBeanMapper mapper = null;

  @PostConstruct
  void init() {
    mapper = new DozerBeanMapper();
  }

  public DozerBeanMapper getMapper() {
    return mapper;
  }

}

And here is a straight forward usecase:

Inject an EJB member variable to your client class:

  @EJB
  DozerInstantiator di;

Within a method somewhere in the client class you can invoke the dozer mapper like so:

Credentials credentials = di.getMapper().map(credentialsDTO, Credentials.class);
// or
Credentials credentials = new Credentials();
di.getMapper().map(credentialsDTO, credentials);

If this is wrong or off base, someone please leave a comment. Until then, this seems to work so I'll use this solution I've developed with Mikko's input.

Bill Rosmus
  • 2,941
  • 7
  • 40
  • 61

1 Answers1

1

If you are using GlassFish 3.x, then you can use EJB 3.1 Singleton Session Bean:

@Startup //initialization in application startup
@Singleton //only one instance 
public class DozerInitializer {
    private String status;

    @PostConstruct //executed once and only once when sole instance is created
    void init {
        //do steps needed to instantiate DozerBeanMapper
        //here
    }
}
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • @"Mikko Maunu" DozerBeanMapper has a method called map(). How would I access it once initialized? The startup has instantiated the DozerInitializer. I would assume that when initializing the DozerBeanMapper class that the DozerBeanMapper class is then a member variable of the initializer class. Is it possible to make use of a static method in the initializer class when calling the 'map' method. DozerInitializer.getDozerInstance.map(); or something like that? – Bill Rosmus Jun 17 '12 at 18:05
  • EJB classes should not contain static methods. There is other ways: l) inject instance of DozerBeanMapper to some instance field of DozerInitializer, provide access via normal EJB business method 2) Use init-method to place sole instance of DozerBeanMapper to the field of some other non-ejb class and let that class provide access to DozerBeanMapper instance. – Mikko Maunu Jun 17 '12 at 18:28
  • @"Mikko Maunu" starting with your suggestion I came up with a working solution. I posted it below my answer and gave you the check mark. Thanks for the help. – Bill Rosmus Jun 27 '12 at 03:27