0

Currently I'm trying to Implement a utility Class that generates an invoice in a PDF Format and I predict that I'll need spring beans to be injected in my Utility Class Afterwards.

But I don't need the class to be instanciated, I only need the methods. So for me it's a dilemma

So I did some research and I still haven't made my mind If I want a spring singleton bean or .

Spring Singleton : Source

@Service
public class Singleton {

private static AtomicReference<Singleton> INSTANCE = new AtomicReference<Singleton>();

public Singleton() {
    final Singleton previous = INSTANCE.getAndSet(this);
    if(previous != null)
        throw new IllegalStateException("Second singleton " + this + " created after " + previous);
}

public static Singleton getInstance() {
    return INSTANCE.get();
}
}

Or A final Class :

 public final InvoiceUtil {

  private InvoiceUtil() {} 

  public static String convertToPDF (Template template) {
    //Do the work
  }

 }

but with the second approach, my class isn't managed by Spring so I can not inject beans to it.

Make me undrestand !! :p

OddDev
  • 1,521
  • 7
  • 23
  • 45
  • 2
    Possible duplicate of [Difference between static class and singleton pattern?](https://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern) – Kaustubh Khare Dec 04 '17 at 10:59
  • I already saw that post, My problem is I don't know which is the best approach in my situation – OddDev Dec 04 '17 at 11:01
  • 1
    If you don't need an instance of something, don't have an instance of it; especially don't use that implementation of singleton, since it doesn't work (`getInstance()` returns null). – Andy Turner Dec 04 '17 at 11:05
  • I don't know about spring beans but afaik if you don't care about the previous state of the program when it is restarted, then create a singleton – denvercoder9 Dec 04 '17 at 11:07
  • @AndyTurner, but how should I make my class final and use static methods and ALSO I make it spring managed (by annotating it with `@Service`) ? – OddDev Dec 04 '17 at 11:10
  • *"But I don't need the class to be instanciated"* Then why are you using an OO programming language in the first place? – Timothy Truckle Dec 04 '17 at 11:11
  • @MoatezBouhdid if you go for the second option, you don't need spring to manage it for you. – Andy Turner Dec 04 '17 at 11:14
  • @AndyTurner, ok, I think you undrestood me, So if I go with the 2nd approach, and I know I'll use a spring service in that class, How should I inject it ? – OddDev Dec 04 '17 at 11:19
  • Possible duplicate of [Java Utility Class vs. Service](https://stackoverflow.com/questions/871434/java-utility-class-vs-service) – tkruse Dec 05 '17 at 01:19

1 Answers1

0

If you use a Spring bean, do not implement Spring Service as a Singleton with getInstance(). Just add @Service and Spring will only instantiate it once.

You can use static methods, and later pass any dependency also to these methods (if those are few dependencies):

public static String convertToPDF (Template template, NeededHelper helper) {
     ...
}
tkruse
  • 10,222
  • 7
  • 53
  • 80