@Profile annotation should be used for loading application configuration based on conditional logic.
Spring has introduced the @Conditional annotation that allows us to define custom conditions to apply to parts of our application context. Spring Boot builds on top of that and provides some pre-defined conditions so we don't have to implement them ourselves.
In Spring Boot, you can use the @ConditionalOnProperty annotation to enable or disable a particular bean based on the presence of a property. This is very useful if you want to provide optional features to your microservice. And that's it. Your optionalClass bean should resolve to null when you specify mybean.
Is it possible to have Java annotations that are applied conditionally? For example, lets say I have a hibernate mapping with an annotated sequence:
@Id
@Column(name = "TABLE_ID")
@GeneratedValue(strategy = SEQUENCE, generator = "generator")
@SequenceGenerator(name = "generator", sequenceName = "TABLE_SEQ")
public Long getId() {
return this.Id;
}
Is it possible to do something like the following, where the annotations are removed based on, say, an environment variable?
@Id
@Column(name = "TABLE_ID")
if(env.equals('dev')){
@GeneratedValue(strategy = SEQUENCE, generator = "generator")
@SequenceGenerator(name = "generator", sequenceName = "TABLE_SEQ")}
public Long getId() {
return this.Id;
}
NOTE: I do understand that in this scenario can use *.hbm.xml files for different environments, however I would like to use annotations as there are less files to maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With