Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement