Solve the problem of injecting Service into null by ordinary class in SpringMvc
- 2021-11-01 03:26:37
- OfStack
The common class in SpringMvc injects Service into null
Scenario:
When using the Quartz timer, the ordinary java class needs to be injected into the spring service class, and an error is reported in the call!
Solution:
/**
* Obtain the course regularly 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;
/**
* In Spring In the annotation, the common class gets the @Service The method of marking or bean Object
*
*/
@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;
}
/**
* Attention bean name Default = Class name ( First letter lowercase ) For example : A8sClusterDao = getBean("a8sClusterDao")
*
* @param name
* @return
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* Obtained from the class 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;// First letter uppercase to lowercase
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);
}
}
At the end of the call, the test can get Service.
The tool class of spring uses service injection
1 needs to inject an service in a tool class with the @ Autowired annotation. However, since tool class method 1 is generally written as static, direct injection is problematic.
Chestnuts:
@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;
}
/**
* SMS history query interface ( Query the SMS sent in a certain time period )
*/
@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
}
}