Reputation: 119
I want to restrict the bean instantiation. Currently, In the web application we are using the spring active profile for client specific bean instantiation. But now i want to restrict some of the bean instantiation specific to environment as well. How can i acheive both at the same time? FYI, i have env build parameter which specify the environment as either dev, qa or prod.
Upvotes: 0
Views: 1217
Reputation: 4024
You can use @Conditional as explained below
Step 1- Implement Condition.matches
so as to specify when should the bean be created or not.
public class SomeCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String env = System.getProperty("env");
return env !=null ? "dev".equals(env); // modify it as per requirement
}
}
Step 2 - In the configuration class specify the above class as condition to decide the bean creation
@Configuration
public class SomeAppConfig {
@Bean
@Condition(SomeCondition.class)
public MyBean myBean() {
return new MyBean();
}
}
P.S.: I assumed that you used Java config and env
as system property.
Upvotes: 2