java - How do I change the property of an enum with Spring? -


i need hide menu options in production environment, not in development.

i implemented enum this:

public enum functionality {     function_1(true),     function_2,     function_3(true);      private boolean usable;      functionality() {         this(false);     }      functionality(boolean usable) {         this.usable = usable;     }      public boolean isusable() {         return usable;     } } 

and then, when need show menu options, check whether functionality needs shown.

so need able change usable boolean when environment development. cannot find way in spring.

do know of way this?

you could change fields of enum, it's considered bad idea , design smell.

a better approach possibly not have usable field @ all, instead make calculated property:

public enum functionality {     function_1(true),     function_2,     function_3(true);      private final boolean restricted;      functionality() {        this(false);     }      functionality(boolean restricted) {         this.restricted = restricted;     }      public boolean isrestricted() {         return restricted;     }      public boolean isusable() {         if (!restricted) {             return true;         } else {             return systemconfiguration.isdevelopmentsystem();         }     } } 

obviously there need method systemconfiguration.isdevelopmentsystem() work.

in systems implemented used enum this:

public enum systemtype {     production,     testing,     development;      public final systemtype current;      static {         string type = system.getenv("system.type");         if ("prod".equals(type)) {             current = production;         } else if ("test".equals(type)) {             current = testing;         } else {             current = development;         }     } } 

here used system property specify type @ runtime, other configuration type might appropriate.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -