工具类中使用spring的bean

1、场景

在开发过程中,有些功能方法适合放置在工具类中。比如日志自定义的appender,它里面的方法调用更适合工具类。

2、方式

2.1 工具类交由spring容器管理

在工具类中定义@Component

package com.ybw.utils;

import com.ybw.dto.PersonDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author ybw
 * @version V1.0
 * @className PersonUtils
 * @date 2022/12/20
 **/
@Component
@Slf4j
public class PersonUtils {

    private static PersonDTO personDTO = null;

    @PostConstruct
    private void init(){
        personDTO=new PersonDTO(1L,"张三");
    }

    public static PersonDTO getPersonDTO() {
        return personDTO;
    }
}

使用方式

PersonDTO personDTO = PersonUtils.getPersonDTO();

2.2 ApplicationContextAware接口实现获取到Spring上下文对象的实例

ApplicationContextAware是一个接口,如果有类(这个类肯定要被Spring扫描到,加上Spring注解,或者Xml配置)继承了这个接口,Spring容器会自动把上下文对象设置到这个类里面,调用setApplicationContext(类实现ApplicationContextAware接口的方法)方法。可以通过Spring上下文对象ApplicationContext获取Spring容器中的Bean。

package com.ybw.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * @author ybw
 * @version V1.0
 * @className SpringContextHolder
 * @date 2022/12/19
 **/
@Slf4j
@Service
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext = null;


    /**
     * 实现ApplicationContextAware接口, 注入Context到静态变量中.
     *
     * @param applicationContext
     * @methodName: setApplicationContext
     * @return: void
     * @author: ybw
     * @date: 2022/12/20
     **/
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }


    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     *
     * @param requiredType
     * @methodName: getBean
     * @return: T
     * @author: ybw
     * @date: 2022/12/20
     **/
    public static  T getBean(Class requiredType) {
        return applicationContext.getBean(requiredType);
    }

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     *
     * @methodName: clearHolder
     * @return: void
     * @author: ybw
     * @date: 2022/12/20
     **/
    public static void clearHolder() {
        if (log.isDebugEnabled()) {
            log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
        }
        applicationContext = null;
    }


    /**
     * 实现DisposableBean接口, 在Context关闭时清理静态变量.
     *
     * @methodName: destroy
     * @return: void
     * @author: ybw
     * @date: 2022/12/20
     **/
    @Override
    public void destroy() {
        SpringContextHolder.clearHolder();
    }
}

工具类调用示例

package com.ybw.utils;

import com.ybw.dto.UserDTO;
import com.ybw.service.UserService;

/**
 * @author ybw
 * @version V1.0
 * @className UserUtils
 * @date 2022/12/19
 **/
public class UserUtils {


    public static UserDTO getUser() {
        return SpringContextHolder.getBean(UserService.class).getUser();
    }
}

你可能感兴趣的:(spring,spring,java,后端)