iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决SpringMvc中普通类注入Service为null的问题
  • 955
分享到

解决SpringMvc中普通类注入Service为null的问题

2024-04-02 19:04:59 955人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

springMVC中普通类注入Service为null 场景: 使用Quartz定时器时,普通的java类需要注入spring的service类,在调用时报错! 解决方式:

springMVC中普通类注入Service为null

场景:

使用Quartz定时器时,普通的java类需要注入spring的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;
 

@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;
    }
 
    
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
 
    
    @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
    }      
}  

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 解决SpringMvc中普通类注入Service为null的问题

本文链接: https://www.lsjlt.com/news/130811.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作