Skip to content

Instantly share code, notes, and snippets.

@mstykt
Created May 5, 2020 08:26
Show Gist options
  • Save mstykt/cb2804b3bbe0d691f0c5994eefe59202 to your computer and use it in GitHub Desktop.
Save mstykt/cb2804b3bbe0d691f0c5994eefe59202 to your computer and use it in GitHub Desktop.
package com.gauge.demo;
import com.thoughtworks.gauge.ClassInitializer;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class RegisterIOC implements ClassInitializer {
private static final ApplicationContext applicationContext;
static {
// Initiate the application context only one-time
applicationContext = new AnnotationConfigApplicationContext(GaugeDemoApp.class);
}
/**
* Determine beans from Spring context when initializing.
*/
@Override
public Object initialize(Class<?> aClass) throws Exception {
String[] beanNames = applicationContext.getBeanNamesForType(aClass);
if (beanNames.length == 0) {
throw new NoSuchBeanDefinitionException(aClass.getName());
}
String s = beanNames[0];
return aClass.cast(applicationContext.getBean(s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment