java ordinary class introduces the pit stepped by Spring container (the injected mapper is null)

The following are all personal experiences of the blogger, hoping to help the students who meet the same problem!

**Background: * * develop a schedule to realize the statistics of various types of abnormal work orders
Implementation mode:
1. Define general interface: abnormal work order statistics
public interface WorkExceptionService {

List<WorkExceptionEntity> getWorkException() throws Exception;

}
2. Define multiple implementation classes, and each implementation class represents an exception work order
@Service("ThirdExceptionWorksImpl ")
public class ThirdExceptionWorksImpl implements WorkExceptionService{

@Autowired
private WorkExceptionMapper mapper;

@Override
public List<WorkExceptionEntity> getWorkException() {
	List<WorkExceptionEntity> thirdList = new ArrayList<>();
	List<WorkExceptionEntity> list  = mapper.getThirdExceptionWorks();
	if(list.isEmpty()) {
		return new ArrayList<>();
	}

3. Method of assigning class object to interface reference

	WorkExceptionService workService ;
	workService = new ThirdExceptionWorksImpl();
	workService.getWorkException();

Note that at this time, Spring @Autowired will not be automatically injected, resulting in calling mapper interface in ThirdExceptionWorksImpl as follows:

List list = mapper.getThirdExceptionWorks();

In this case, the mapper is null. If NULL calls the getthirdexeptionworks(); method after null, an error must be reported.

terms of settlement:
First of all, we need to introduce the Spring container help class (SpringContextUtil) to implement the ApplicationContextAware interface

import org.springframework.beans.BeansException;
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;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	if (SpringContextUtil.applicationContext == null) {
		SpringContextUtil.applicationContext = applicationContext;
	}
}

/**
 * Get applicationContext
 * 
 * @return
 */
public static ApplicationContext getApplicationContext() {
	return applicationContext;
}

/**
 * Get Bean through name
 * 
 * @param name
 *            Bean's name
 * @return
 */
public static Object getBean(String name) {
	return getApplicationContext().getBean(name);
}

/**
 * Get Bean through class
 * 
 * @param clazz
 * @return
 */
public static <T> T getBean(Class<T> clazz) {
	return getApplicationContext().getBean(clazz);
}

/**
 * Return the specified Bean through name and Clazz
 * 
 * @param name
 * @param clazz
 * @return
 */
public static <T> T getBean(String name, Class<T> clazz) {
	return getApplicationContext().getBean(name, clazz);
}

}
As you can see from this implementation class, there are three ways to get beans:
1. Get the Bean by implementing the name of the class
2. Get Bean through class
3. Return the specified Bean through name and class

Here I take the way to get Bean by implementing the name of class as an example. The other two ways are left to the interested students to study.
Specific use:
1. Declare the interface object (workService) in the referenced class, and specify the specific implementation class (thirdexeptionworksimpl):
WorkExceptionService workService = (WorkExceptionService) SpringContextUtil.getBean("ThirdExceptionWorksImpl");

2. Call the interface that implements class Rewriting:
WorkExceptionService workService = (WorkExceptionService) SpringContextUtil.getBean("ThirdExceptionWorksImpl");
workService.getWorkException();

3. Add a comment on the implementation class: @ Service("thirdexeptionworksimpl"), specify its implementation class, customize its name, and make sure it is consistent with that in. getBean("") in 1.


In this way, we can solve this problem perfectly!!!

Published 1 original article, praised 0, visited 10
Private letter follow

Tags: Spring

Posted on Mon, 13 Jan 2020 21:35:37 -0500 by goldilok