解决SpringMvc中普通类注入Service为null的问题
作者:猫猫桑 时间:2023-10-29 01:23:25
SpringMvc中普通类注入Service为null
场景:
使用Quartz定时器时,普通的java类需要注入spring的service类,在调用时报错!
解决方式:
/**
* 定时获取课程的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标记的方法或者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默认 = 类名(首字母小写) 例如: A8sClusterDao = getBean("a8sClusterDao")
*
* @param name
* @return
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 根据类名获取到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);
}
}
调用结束,测试可以获取Service.
spring之工具类使用service注入
一般需要在一个工具类中使用@Autowired 注解注入一个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;
}
/**
*短信历史查询接口(查询某个时间段发送的短信)
*/
@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
}
}
来源:https://blog.csdn.net/u011429743/article/details/81111151
标签:SpringMvc,注入,Service,null
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Retry 重试实例详解
2021-07-15 22:43:26
![](https://img.aspxhome.com/file/2023/5/63495_0s.png)
一文详解Java中Stream流的使用
2021-08-23 11:54:51
![](https://img.aspxhome.com/file/2023/0/64800_0s.png)
java io读取文件操作代码实例
2023-04-12 08:53:57
android自定义控件实现简易时间轴(1)
2022-09-24 00:42:23
![](https://img.aspxhome.com/file/2023/0/139390_0s.gif)
深入理解Spring AOP
2023-02-09 15:14:40
flutter使用tauri实现一个一键视频转4K软件
2022-10-23 05:46:42
java加密算法分享(rsa解密、对称加密、md5加密)
2021-08-30 16:22:08
Java内存缓存工具Guava LoadingCache使用解析
2023-05-25 08:36:36
C# 使用Dictionary复制克隆副本及比较是否相等
2021-05-29 21:33:06
关于Mybatis-Plus字段策略与数据库自动更新时间的一些问题
2023-08-05 20:44:22
AOP之事务管理<aop:advisor>的两种配置方式
2023-11-24 22:55:06
C++ 异常处理 catch(...)介绍
2023-06-24 05:35:32
Android App获取屏幕旋转角度的方法
2021-12-26 10:42:19
![](https://img.aspxhome.com/file/2023/1/91701_0s.gif)
Android为View添加拖放效果的方法实例
2023-06-25 18:53:45
java遍历Map的几种方法分析
2023-12-04 22:34:27
Javaweb监听器实例之统计在线人数
2023-02-08 11:47:23
![](https://img.aspxhome.com/file/2023/6/75006_0s.jpg)
SSH框架网上商城项目第8战之查询和删除商品类别功能实现
2023-02-12 05:54:39
![](https://img.aspxhome.com/file/2023/8/81188_0s.jpg)
java笔记学习之操作符
2022-10-19 05:31:59
浅谈Android onTouchEvent 与 onInterceptTouchEvent的区别详解
2021-05-30 02:56:55
![](https://img.aspxhome.com/file/2023/1/114331_0s.jpg)
C#实现递归调用的Lambda表达式
2022-10-03 05:13:29