服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 解决SpringMvc中普通类注入Service为null的问题

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

2021-10-18 10:28猫猫桑 Java教程

这篇文章主要介绍了解决SpringMvc中普通类注入Service为null的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringMvc中普通类注入Service为null

场景:

使用Quartz定时器时,普通的java类需要注入spring的service类,在调用时报错!

解决方式:

?
1
2
3
4
5
6
/**
 * 定时获取课程的service
 */
@Autowired
protected QuartzGetCourseService quartzGetCourseService = (QuartzGetCourseService) SpringContextUtil
        .getBean("quartzGetCourseService");
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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,所以直接注入就存在问题。

栗子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@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

延伸 · 阅读

精彩推荐