您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何解決SpringMvc中普通類注入Service為null的問題”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
使用Quartz定時(shí)器時(shí),普通的java類需要注入spring的service類,在調(diào)用時(shí)報(bào)錯(cuò)!
/** * 定時(shí)獲取課程的service */ @Autowired protected QuartzGetCourseService quartzGetCourseService = (QuartzGetCourseService) SpringContextUtil .getBean("quartzGetCourseService");
import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 在Spring 注解中,普通類獲取@Service標(biāo)記的方法或者bean對象 * */ @Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 注意 bean name默認(rèn) = 類名(首字母小寫) 例如: A8sClusterDao = getBean("a8sClusterDao") * * @param name * @return * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 根據(jù)類名獲取到bean * * @param <T> * @param clazz * @return * @throws BeansException */ @SuppressWarnings("unchecked") public static <T> T getBeanByName(Class<T> clazz) throws BeansException { try { char[] cs = clazz.getSimpleName().toCharArray(); cs[0] += 32;// 首字母大寫到小寫 return (T) applicationContext.getBean(String.valueOf(cs)); } catch (Exception e) { e.printStackTrace(); return null; } } public static boolean containsBean(String name) { return applicationContext.containsBean(name); } public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } }
調(diào)用結(jié)束,測試可以獲取Service.
一般需要在一個(gè)工具類中使用@Autowired 注解注入一個(gè)service。但是由于工具類方法一般都寫成static,所以直接注入就存在問題。
栗子:
@Component public class SmsController { private static Logger logger = LoggerFactory.getLogger(SmsController.class); @Autowired private MessagesInfoService messagesInfoService; private static SmsController smsController; @PostConstruct public void init() { smsController = this; smsController.messagesInfoService = this.messagesInfoService; } /** *短信歷史查詢接口(查詢某個(gè)時(shí)間段發(fā)送的短信) */ @RequestMapping(value = "/queryMessage",method = RequestMethod.GET) public ModelAndView queryMessage{ pager = messagesInfoService.findPager(map,5,pIndex); ModelAndView modelAndView = new ModelAndView("manage/jgdxgl/jgdx_qm"); List<MessagesInfo> list = pager.getItem(); modelAndView.addObject("pager",pager); modelAndView.addObject("list",list); return modelAndView } }
“如何解決SpringMvc中普通類注入Service為null的問題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。