如何在工具类中调用DB接口
category: arch tags: [arch] keywords: 架构
以下文章来源于随★风
正文
问题:springboot 项目中在普通类中调用dao层的mapper 出现空指针异常
解决方式一、该类使用@Component注解
添加一个本类类型的静态字段
创建一个初始化方法,贴上@PostConstruct 标签,用于注入bean
创建方法调用mapper或service接口
最后直接在普通类中调用即可
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
个人理解就是mapper 装载失败 重点init()方法中转了一次
@Component
public class TestUtil {
@Autowired private DoctorInformationMapper doctorInformationMapper; public static TestUtil testUtil;
@PostConstruct public void init() {
testUtil = this;
testUtil.doctorInformationMapper = this.doctorInformationMapper;
} //调用
public static int getOpenId() { int count = testUtil.doctorInformationMapper.count("12313"); return count;
}
}
解决方式二、通过spring 容器去拿
2.1首先打印出Spring容器所有的Bean名称
@Component
public class ApplicationContextBean implements ApplicationContextAware, InitializingBean {
public static ApplicationContext applicationContext;
/**
* 获取 ApplicationContext
*
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextBean.applicationContext = applicationContext;
}
/**
* 打印IOC容器中所有的Bean名称
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
String[] names = applicationContext.getBeanDefinitionNames();
for (String name : names) {
System.out.println(">>>>>>" + name);
}
System.out.println("------\nBean 总计:" + applicationContext.getBeanDefinitionCount());
}
}
不用调用测试,项目启动就会在后台打印出来
2.2开始写获取bean工具类
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public SpringUtil() {
}
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static void setAppCtx(ApplicationContext webAppCtx) {
if (webAppCtx != null) {
applicationContext = webAppCtx;
}
}
/**
* 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {
return getApplicationContext().getBean(name, clazz);
}
public static final Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
Class clz = Class.forName(className);
return getApplicationContext().getBean(beanName, clz.getClass());
}
public static boolean containsBean(String name) {
return getApplicationContext().containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().isSingleton(name);
}
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().getAliases(name);
}
//测试
public static String getCharId(String chatId) {
ChatListAssociationMapper associationMapper = (ChatListAssociationMapper) SpringUtil.getBean("chatListAssociationMapper");//这个参数是spring容器里存在的,名字最好复制,大小写要一致,不然会失败
return associationMapper.chatId(chatId);
}
}
注意以上的两种方法都不能在main方法调用,会报空指针异常