Pergunta

I have a simple old style static methods in a utility class. I am struggling to "CDI" it due to the paramterised objects required each time..any help appreciated on how best to approach this?

i.e. i would like to Inject the utility class into multiple classes and call the methods below (and similar) where required...The utility class would in turn inject the ByteArrayInputStream,ObjectInputStream each time with correct parameters...

Old class:

public class Utils {


      public static Object fromByteArray(final byte[] data) throws IOException, ClassNotFoundException {

                final ByteArrayInputStream b = new ByteArrayInputStream(data);
                final ObjectInputStream o = new ObjectInputStream(b);
                return o.readObject();
      }


      public static byte[] toByteArray(final MyObj tx)  throws IOException{
                final ByteArrayOutputStream b = new ByteArrayOutputStream();
                final ObjectOutputStream o = new ObjectOutputStream(b);
                o.writeObject(tx);
                return b.toByteArray();
      }

}

Thanks...

Foi útil?

Solução

There are multiple ways to accomplish this. Two possibilities:

Simply use this in the method/constructor of your bean:

Object object = Utils.fromByteArray(...);

Inject it:

@Inject
private Utils utils;

You don't need to modify your class Utils at all.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top