springboot project null pointer exception in the general category mapper call dao layer

Project I encountered the same problem about special record

There are two ways

 

One,

This class uses the annotation @Component

Adding a class of this type of static field

Create an initialization method, @PostConstruct label affixed to inject bean

Create a mapper or service method invocation interfaces

The last direct calls to regular classes

@Component refers to the component when the component is not classified, we can use this annotation to mark.

 @PostConstruct be modified method runs when you load the Servlet server, and the server will only be called once, similar to Serclet of inti () method. @PostConstruct modified method will be run before the init () method after the constructor.

Personal understanding is the focus mapper failed to load init () method a transit

@Component
public class TestUtil {

    @Autowired
    private DoctorInformationMapper doctorInformationMapper;


    public static TestUtil testUtil;

    @PostConstruct
    public void init() {
        testUtil = this;
        testUtil.doctorInformationMapper = this.doctorInformationMapper;
    }

    //调用
    public static int getOpenId() {
        int count = testUtil.doctorInformationMapper.count("12313");
        return count;
    }
    
}

 

two. Pick up by spring container

2.1 First print out all the Spring container Bean name

@Component
public class ApplicationContextBean implements ApplicationContextAware, InitializingBean {

    public static ApplicationContext applicationContext;

    /**
     * 获取 ApplicationContext
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextBean.applicationContext = applicationContext;
    }

    /**
     * 打印IOC容器中所有的Bean名称
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(">>>>>>" + name);
        }
       System.out.println("------\nBean 总计:" + applicationContext.getBeanDefinitionCount());
    }

}

Instead of calling test, project start will be printed in the background

 

2.2 bean get started writing tools

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    public SpringUtil() {
    }

    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        if (applicationContext == null) {
            applicationContext = arg0;
        }

    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    } 

    Public  static  void setAppCtx (ApplicationContext webAppCtx) {
         IF (webAppCtx =! Null ) { 
            applicationContext = webAppCtx; 
        } 
    } 

    / ** 
     * get ApplicationContext object instance after the instance of the object can be acquired manually injected into the Bean 
     * / 
    public  static <T> the getBean T (Class <T> clazz) {
         return getApplicationContext () the getBean (clazz);. 
    } 

    public  static <T> T the getBean (String name, Class <T> clazz) throws a ClassNotFoundException {
         return getApplicationContext().getBean(name, clazz);
    }

    public static final Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
    }

    public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
        Class clz = Class.forName(className);
        return getApplicationContext().getBean(beanName, clz.getClass());
    }

    public static boolean containsBean(String name) {
        return getApplicationContext().containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return getApplicationContext().isSingleton(name);
    }

    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return getApplicationContext().getType(name);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return getApplicationContext().getAliases(name);
    }
  
//测试 public staticGetCharId String (String ChatID) { ChatListAssociationMapper associationMapper = (ChatListAssociationMapper) SpringUtil.getBean ( "chatListAssociationMapper" ); // this parameter is spring container exists, the name of the best copy, the case to be consistent, otherwise it will fail return associationMapper.chatId (ChatID); }
}

 

Note that the above two methods can not be invoked in main method, will be reported null pointer exception

Guess you like

Origin www.cnblogs.com/nongzihong/p/11350308.html