Springboot @Autowired failed to inject problem

Keywords: Java Spring

Special note: pay attention to the file structure

WebappApplication must be at the outermost layer of the package, otherwise Spring cannot host all classes, which will cause @ Autowired to fail to inject.

1. Add a tool class to get the Bean hosted in Spring

(1) tools

package com.common;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @program: IPC_1P
 * @description: Get bean s hosted in spring
 * @author: johnny
 * @create: 2018-08-03 16:24
 **/
public class SpringContextUtil {
    private static ApplicationContext applicationContext; // Spring Application context

    // The following method is added@Override Comment, because inheritance ApplicationContextAware Interface is a must

    public static 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);
    }

    public static Object getBean(String name, Class requiredType)
            throws BeansException {

        return applicationContext.getBean(name, requiredType);
    }

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

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

    public static Class getType(String name)
            throws NoSuchBeanDefinitionException {
        return applicationContext.getType(name);
    }

    public static String[] getAliases(String name)
            throws NoSuchBeanDefinitionException {
        return applicationContext.getAliases(name);
    }
}

(2) use

1) when the program starts, instantiate SpringContextUtil

@SpringBootApplication
public class WebappApplication {

    private static ApplicationContext applicationContext;
    public static void main(String[] args) {
        applicationContext = SpringApplication.run(WebappApplication.class, args);

        //
        SpringContextUtil springContextUtil = new SpringContextUtil();
        springContextUtil.setApplicationContext(applicationContext);

        System.out.println("Server start test!");
}

 

2) in the method of using @ Service, inject @ Autowired and use SpringcontexUtil to get the Bean context

@Autowired
    SenderService senderService;

public class Package_State  {

    @Autowired
    SenderService senderService;
    
    @Component
    private Package_State() {
        senderService = (SenderService)SpringContextUtil.getBean("senderService");
    }
}

 

Java is a beginner. The understanding of the principle is not very thorough. It only records the problems encountered. It can be modified at any time with the deepening of learning

Posted by pradee on Mon, 06 Jan 2020 13:03:54 -0800