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?