Created
May 5, 2020 08:26
-
-
Save mstykt/cb2804b3bbe0d691f0c5994eefe59202 to your computer and use it in GitHub Desktop.
This file contains 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
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