@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.context = applicationContext;
}
/**
* 根据bean名字获取工厂中指定bean 对象
*/
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
}
service类:
@Service("ReportService")
public class ReportService {
public void test(String logText) {
//略...
}
}
普通类:
ReportService reportService=(ReportService) ApplicationContextUtil.getBean("ReportService");
reportService.test("job ok");