Last active
July 26, 2017 10:26
-
-
Save ksaua/fd63bf6a7dce24958e88 to your computer and use it in GitHub Desktop.
@Profile equivalent for Constretto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@WithConstrettoTag("development") | |
public class Example { | |
@Bean | |
public DataSource developmentDataSource() { | |
// This bean will only be created with development constretto tag | |
(...) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import org.springframework.context.annotation.Conditional; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ ElementType.TYPE, ElementType.METHOD }) | |
@Documented | |
@Conditional(WithConstrettoTagCondition.class) | |
public @interface WithConstrettoTag { | |
/** | |
* The set of constretto tags for which the annotated component should be registered. | |
*/ | |
String[] value(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.springframework.context.annotation.Condition; | |
import org.springframework.context.annotation.ConditionContext; | |
import org.springframework.core.type.AnnotatedTypeMetadata; | |
import org.springframework.util.MultiValueMap; | |
public class WithConstrettoTagCondition implements Condition { | |
@Override | |
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | |
String constrettoTags = System.getProperty("CONSTRETTO_TAGS"); | |
if (constrettoTags != null) { | |
MultiValueMap<String, Object> attrs = metadata | |
.getAllAnnotationAttributes(WithConstrettoTag.class.getName()); | |
if (attrs != null) { | |
for (Object value : attrs.get("value")) { | |
if (constrettoTags.contains(((String[]) value)[0])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment