Skip to content

Instantly share code, notes, and snippets.

@p1coderblog
Created July 3, 2019 01:10
Show Gist options
  • Save p1coderblog/7f54f8cb6ac036473bd368be9f29929c to your computer and use it in GitHub Desktop.
Save p1coderblog/7f54f8cb6ac036473bd368be9f29929c to your computer and use it in GitHub Desktop.
AutowireUtil class is used to auto-wire spring-beans inside JPA listener
package com.example.demo.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public final class AutowireUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
private AutowireUtil() {
}
public static void autowire(Object classToAutowire, Object... beansToAutowireInClass) {
for (Object bean : beansToAutowireInClass) {
if (bean == null) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(classToAutowire);
return;
}
}
}
@Override
public void setApplicationContext(final ApplicationContext applicationContext) {
AutowireUtil.applicationContext = applicationContext;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment