Created
August 21, 2017 06:28
-
-
Save banterCZ/ed7786cf1c188dd2da098cd9fc2cc4b0 to your computer and use it in GitHub Desktop.
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
import javax.annotation.PostConstruct; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.stereotype.Component; | |
/** | |
* Removes the specified beans from spring context. | |
* | |
* @author banterCZ | |
*/ | |
@Component | |
public class BeanRemover { | |
private static final Logger logger = LoggerFactory.getLogger(BeanRemover.class); | |
@Autowired | |
private ApplicationContext ctx; | |
@PostConstruct | |
public void unregisterBeans () { | |
//TODO replace bean lookup whatever you want | |
final String[] beanNames = ctx.getBeanNamesForType(BeanToRemove.class); | |
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ctx.getAutowireCapableBeanFactory(); | |
for (String beanName : beanNames) { | |
logger.debug("Going to unregister bean '{}'", beanName); | |
registry.removeBeanDefinition(beanName); | |
logger.info("Unregistered bean '{}'", beanName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment