Home > Blockchain >  Spring Service or Util for a generator class in Java?
Spring Service or Util for a generator class in Java?

Time:12-19

I have barcode generator methods that I call from Controller in my Java (Spring Boot) project. The generator class is as shown below:

@Component
@NoArgsConstructor
public class QRCodeGenerator {
    private static final int LABEL_X_POS = 45;
    private static final int LABEL_Y_POS = 20;

    @Value("${qr-code.margin}")
    private int margin;

    @Value("${qr-code.fontSize}")
    private int fontSize;

    public ResponseEntity<Resource> getQRCode(String data) throws IOException {
        
        // code omitted for brevity
        
        addLabel(image, label);        
        final ByteArrayResource resource = new ByteArrayResource(toByteArray(image));
        return ResponseEntity.ok().body(resource);
    }

    private static byte[] toByteArray(BufferedImage image) throws IOException {
        // code omitted for brevity
    }

    private void addLabel(BufferedImage source, String text) {
        int x = LABEL_X_POS;
        int y = LABEL_Y_POS;
        // code omitted for brevity
    }
}

First I started to use this class as static, but then I removed static to read the data in application.yml properly.

Here is some points that I need to be clarified:

1. I am new in Spring and as far as I see, spring services are generally used to provide data from database or any other service, endpoints, etc. Is that true?

2. Should I create a service for the class above (I do not need to access to database in that class)? Or it the approach is better (creating as a component and then injecting it to my Controller)?

3. There are many different opinions regarding to static Util classes. So, what about converting this class to a static Util class? Is it better than now or using it as a Spring Service?

CodePudding user response:

Here are some opinions on your questions:

  1. Classes annotated with @Service are used in business logic layer, it can provide data but it can also do actions as in your case.
  2. Based on the point 1. you can make it a service because that provides logic for creating qr codes for representation layer (Controllers).
  3. Having this kind of classes as @Component or more specific @Service makes it easier to inject dependencies using Dependency Injection and also it's easier to inject your class as dependency to other beans so it's easier also to test/mock. Keeping it as Spring Service is a better approach.

CodePudding user response:

Make it a spring service, and you will be able to substitute a mock for it easily when you write a test for the controller. Otherwise you will not be able to mock it (unless you use things like Powernock). Since you're already injecting values into it, it's a lot easier to keep this as a Spring component.

Generally it's a premature optimization to use static util classes and it can get in the way of testing. I would use static util classes only if they contain only static methods that have no side effects, which don't need access to configuration data and which I don't mind not being able to mock. The methods I use in input validation code would be an example of something I might use static methods for. But you have the most flexibility, for instance to be able to inject configurations, if you keep these as Spring components.

  • Related