Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created June 22, 2012 16:20
Show Gist options
  • Save dwelch2344/2973807 to your computer and use it in GitHub Desktop.
Save dwelch2344/2973807 to your computer and use it in GitHub Desktop.
Autowiring all defined beans
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class DanTest implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();
for(String name : ctx.getBeanDefinitionNames()){
Object bean = ctx.getBean(name);
System.out.println( String.format("Trying to autowire: %s of type %s", name, bean) );
bf.autowireBean(bean);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment