我也不知道为什么,一觉醒来就报错了
今日任务:
排查项目细节,修复小bug
早上醒来之后,运行项目,突然就报错了。本来不清醒的脑袋就更加嗡嗡的。错误提示是com.hp.util.SpringContextHolder not found。很明显,这是spring容器在初始化类的时候没找到路径。


我眉头一皱,觉得使用bean标签和注解,功能冲突,于是乎,我把bean标签注释掉,他能运行了。
按照剧本,应该是普天同庆,载歌载舞。但是,我把bean标签的注释去掉,再运行,它还可以完美运行!!!!!!那早上哪个错误是什么?是上天对我的考验?还是完美的误会?
下边说一下SpingContextHolder这个类。
代码如下:
package com.hp.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component @Lazy(false) public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextHolder.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { assertApplicationContext(); return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String beanName) { assertApplicationContext(); return (T) applicationContext.getBean(beanName); } public static <T> T getBean(Class<T> requiredType) { assertApplicationContext(); return applicationContext.getBean(requiredType); } private static void assertApplicationContext() { if (SpringContextHolder.applicationContext == null) { throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了SpringContextHolder!"); } } }
我想在一个工具类中调用dao层的接口方法,但是ssm并未注入,于是就用到了这个类
@Autowired private ItemBankDao itemBankDao = SpringContextHolder.getBean(ItemBankDao.class); @Autowired private TestPaperDao testPaperDao = SpringContextHolder.getBean(TestPaperDao.class);
工具类中这样写,就可以调用dao层接口了。
正经人谁写日记啊。。。
浙公网安备 33010602011771号