1

Is there any annotation in Spring to instantiate util class. I have util class which is used by service layer.

Util Class

Class CipherUtil {
  private static final String  SECRET_KEY = "sEcrEtkEy";
  private static Cipher cipher;
  private static SecretKeySpec secretKeySpec;

  public CipherUtil() {
    //initilize cipher and secretKeySpec properties
  }

  public static String encrypt(String plainText) { ... }

  public static String decrypt(String encryptedText) { ... }
}

Service Class

Class MyService {
  @Autowired OtherService otherService;

  public void doSomething() {
      //CipherUtil is null
      String decryptedMsg = CipherUtil.encrypt("Hello World!!!"); 
  }
}

How I can tell Spring to create CipherUtil class before I use in method?

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95

2 Answers2

5

Checkstyle has a nice rule for Util classes, which states:
Utility classes should not have any non-private constructors and should be final.

This is because you don't want shadowing to mess with your methods and because if you need a constructor then it's not a utility class.

Going back to your example:

Having a constructor initialize static fields is BAD.

Why? Because they are mutable.

You are specifically saying that they can change by making them non-final. So, suppose your method is doing something with them, changing their values, and then another thread goes in and creates another instance of your object while that method is running. And hence - resetting the static fields. Your method suddenly breaks horribly without any apparent reason and without you being able to reproduce it.

A better way to do this is to make the fields final. If they are immutable, then there is no danger. But how do you initialize them? You can't use a constructor (compiler error). What can help is static initializers. They are run once when the class is loaded and have full access to static fields. You may want to use one block to set your values.

If the fields are mutable, then making them static and initialize in a constructor is the worst thing you can do. Rethink your design. Otherwise you are opening a whole new can of worms in threading problems.

A word of warning:

I'm not sure by the code you've given, but it seems to me that using a static-singleton util class is a bad idea here. Most apps will want to change keys every now and then, and non-trivial logic in a static class is very difficult to test around. There are better ways of doing this, the most straightforward of which - make your CipherUtil an actual bean.

Community
  • 1
  • 1
Ordous
  • 3,844
  • 15
  • 25
1

Java classes are package private by default.

Refer to: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Make your utils class public final.

public final class CipherUtil { ... }

Also, if you want to initialize any static class variables, you would need to use the static initializer.

public final class CipherUtil {

  private static final String SECRET_KEY;

  // Can't be instantiated 
  private CipherUtil(){ }

  static{
    //Initialize stuff here
    SECRET_KEY = "sEcrEtkEy";
  }

}
proulxs
  • 498
  • 2
  • 13
  • Java classes are package-private by default, just like their members. These Utils can still be used in the same package in the OPs version (though they likely meant them to be `public`) – Ordous Sep 24 '14 at 17:15
  • _Java classes are private by default_ is false, but how would it even be relevant here? – Sotirios Delimanolis Sep 24 '14 at 17:15
  • Pardon my ignorance, I meant to say package private. Edited. I would also normally declare the class as final for intent and also declare the constructor to be private to avoid any instance creation. – proulxs Sep 24 '14 at 17:22
  • @proulxs Ah, there have been so many discussions on what's best practice for singletons. IMHO the `enum` solution is best when you don't need lazy init and I try to use that. But that has little to do with the OPs question. – Ordous Sep 24 '14 at 17:45