在監聽中是無法直接根據註解或者是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");