Spring Boot Configuration Profiles and @TypeDefs
<p>Fellow SO-er's:</p>

<p>I've been puzzling over this one for a couple of days, and, as of yet, don't have a solution ...</p>

<p>I'm building a Spring Boot web app and what I'd like to be able to do is to activate/deactivate encryption of data fields in my datastore (using the facilities provided by jasypt+spring+hibernate) via activating/deactivating configuration profiles. So that - for development work - I can have data fields stored as clear text, while for production, they would be encrypted.</p>

<p>Currently, I'm doing this via a rather inelegant approach. Specifically, I comment/uncomment code in my package-info.java file where I define two @TypeDefs annotation blocks for the type used for the datastore field- one of which is commented and the other of which is active. Thus, my current package-info.java file is written as follows:</p>

<pre><code>//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use this @TypeDefs annotation when dataencrypt configuration profile is active
//@TypeDefs({ @TypeDef(name = com.castlehillgaming.gameshare.model.Ticket.ENCRYPTED_STRING_TYPENAME, typeClass = EncryptedStringType.class, parameters = {
//        @Parameter(name = "encryptorRegisteredName", value = com..evilcorp.evilproject.config.EncryptionConfig.REGISTERED_NAME) }) })
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use this @TypeDefs annotation when dataencrypt configuration profile is not active
@TypeDefs({
        @TypeDef(name = com.evilcorp.evilproject.model.Ticket.ENCRYPTED_STRING_TYPENAME, typeClass = String.class, parameters = {
                @Parameter(name = "encryptorRegisteredName", value = com..evilcorp.evilproject.config.EncryptionConfig.REGISTERED_NAME) }) })
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com..evilcorp.evilproject.model;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.jasypt.hibernate4.type.EncryptedStringType;
</code></pre>

<p>And my @Entity Ticket class contains the following:</p>

<pre><code>@Entity
@EqualsAndHashCode(of = { "ticketId" })
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
public class Ticket implements Serializable {

    ...

    @Column(unique = true, nullable = false)
    @Type(type = ENCRYPTED_STRING_TYPENAME)
    private @Getter String ticketId;

    ...
}
</code></pre>

<p>I'm hoping that I can devise something that will allow me to reduce my package-info.java file to the following:</p>

<pre><code>@TypeDefs({
        @TypeDef(name = com.evilcorp.evilproject.model.Ticket.ENCRYPTED_STRING_TYPENAME, typeClass = com.evilcorp.evilproject.config.MyTicketDataFieldString.class, parameters = {
                @Parameter(name = "encryptorRegisteredName", value = com..evilcorp.evilproject.config.EncryptionConfig.REGISTERED_NAME) }) })

package com..evilcorp.evilproject.model;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
</code></pre>

<p>And define two distinct versions of MyTicketDataFieldString class based on the state of Spring Boot Configuration Profiles. E.g., </p>

<pre><code>@Configuration
@Profile("dataencrypt")
public class MyTicketDataFieldString extends EncryptedStringType {}
</code></pre>

<p>and</p>

<pre><code>@Configuration
@Profile("!dataencrypt")
public class MyTicketDataFieldString implements CharSequence { ... }
</code></pre>

<p>where the CharSequence implementation behaves like a vanilla java.lang.String.</p>

<p>But, this won't work because I'll have to define the same class twice in a specific package.</p>

<p>Any ideas on how this can be done (or something equivalent) would be much appreciated.</p>