I have a simple implementation AttributeConverterin which I am trying to introduce an object that should provide the conversion logic, but @Injectdoes not seem to work for this case. The converter class is as follows:
@Converter(autoApply=false)
public class String2ByteArrayConverter implements AttributeConverter<String, byte[]>
{
@Inject
private Crypto crypto;
@Override
public byte[] convertToDatabaseColumn(String usrReadable)
{
return crypto.pg_encrypt(usrReadable);
}
@Override
public String convertToEntityAttribute(byte[] dbType)
{
return crypto.pg_decrypt(dbType);
}
}
When triggered, @Converterit throws NullPointerException, because the property is cryptonot initialized from the container. Why is this?
I use Glassfish 4, and in all other cases it @Injectworks just fine.
Is it impossible to use CDI on converters?
Any help would be appreciated :)
- AttributeConverter. , CDI bean http://docs.oracle.com/javaee/6/tutorial/doc/gjfzi.html.
CDI , :
@Inject
public String2ByteArrayConverter(Crypto crypto)
{
this.crypto = crypto;
}
, :
2015-07-23T01:03:24.835+0200|Severe: Exception during life cycle processing
org.glassfish.deployment.common.DeploymentException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [PU_VMA] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-7172] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Error encountered when instantiating the class [class model.converter.String2ByteArrayConverter].
Internal Exception: java.lang.InstantiationException: model.converter.String2ByteArrayConverter
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
...
@Producer @Decorator, CDI , , - AttributeConverter, CDI. .