監聽中如何調用業務類

在監聽中是無法直接根據註解或者是new調用業務邏輯層的,那麼需要通過spring的appliactionContext來獲取

一、編寫一個工具類SpringUtils

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;


@Service
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;

public static Object getBean(String name) {
if (applicationContext == null) {
return null;
}
return applicationContext.getBean(name);
}


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
}

二、調用方式

@Service(value="testServiceImpl")
public class TestServiceImpl extends BaseServiceImpl<Test> implements
TestServiceI


TestServiceI tService = (TestServiceI)SpringUtils.getBean("testServiceImpl");


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章