You can use this way to use spring bean in non-spring bean class
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext appContext) {
ctx = appContext;
}
public static ApplicationContext getApplicationContext() {
return ctx;
}
}
now you can get the applicationcontext object by getApplicationContext() this method.
from applicationcontext you can get spring bean objects like this:
ApplicationContext appCtx = ApplicationContextUtils.getApplicationContext();
String strFromContext = appCtx.getBean(beanName, String.class);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…