SpringBoot启动类中注入service

一、前言

SpringBoot集成mybatis, 一般项目起步阶段在启动类中进行接口测试还是很方便的,因此在SpringBoot中就会注入service,但是总是null,注入失败,什么原因呢?

1、在应用的Filter或Listener中使用了@Autowired 

原因:因为Filter和Listener加载顺序优先于spring容器初始化实例,所以使用@Autowired肯定为null了~~

解决:用ApplicationContext根据bean名称(注意名称为实现类而不是接口)去获取bean,随便写个工具类即可

2、没加@Service注解等 ,这一类低级错误自己检查即可

3、你写的@Service、@Componet、@Configuration、@Repository等Spring注解未被扫描到,例如:springboot的主类扫描规则

4、@Autowired注入时还未进行Bean初始化

二、冗余的二次初始化

我们可以进行xml加载并进行初始化

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-common.xml");  
UserService userService = (UserService)appContext.getBean("userService");  

但是这样就会存在一个问题:因为它会重新装载applicationContext-common.xml并实例化上下文bean,如果有些线程配置类也是在这个配置文件中,那么会造成做相同工作的的线程会被启两次。一次是web容器初始化时启动,另一次是上述代码显示的实例化了一次。当于重新初始化一遍!!!!这样就产生了冗余。

三、解决办法ApplicationContextAware

不用类似new ClassPathXmlApplicationContext()的方式,从已有的spring上下文取得已实例化的bean。通过ApplicationContextAware接口进行实现。

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象。

研究了一天,终于百度到了,都是固定写法!!!

package com.util;

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 = null;

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

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

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

    public static  Object getBean(Class clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static  Object getBean(String name, Class clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}
@Autowired
private static UserService userService;

public static void main(String[] args) {
    SpringApplication.run(DBApplication.class,args);
    ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
    userService = applicationContext.getBean(UserService.class);
    userService.selectUser("zs");
}

1、在spring的配置文件中,注册方法类SpringContextUtil。之所以方法类SpringContextUtil能够灵活自如地获取ApplicationContext,就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。这里我们使用@Component来进行注册,或者我们也可以像下面这样在配置文件声明bean:

2、加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的。

public void setApplicationContext(ApplicationContext context) throws BeansException

 方法,获得ApplicationContext对象,ApplicationContext对象是由spring注入的。前提必须在Spring配置文件中指定该类。

3、使用静态的成员ApplicationContext类型的对象。

 

上一篇:【全栈最全Java框架总结】SSH、SSM、Springboot

下一篇:超详细的springBoot学习笔记

你可能感兴趣的:(Spring,Boot)