Spring Boot或Cloud的普通类如何获取service实现

Spring Boot或Cloud的普通类如何获取service实现

我们在便利使用SpringBootSpringCloud的时候,很多时候会遇到一种场景,想在utils的工具类中调用被Spring托管的service.那么我们该如何优雅的实现呢?

Num1: 使用PostConstruct

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderUtils {

    @Autowired
    private OrderService orderService;

    public static OrderUtils orderUtils;

    public void setService(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 核心
     */
    @PostConstruct
    public void init() {
        orderUtils = this;
        orderUtils.orderService = this.orderService;
    }

    public static OrderVo getOrder(String orderId) throws Exception{
        OrderVo order= orderUtils.orderService.getOrderById(orderId);
        return order;
    }

}

@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。因为注解@PostConstruct的缘故,在类初始化之前会先加载该使用该注解的方法;然后再执行类的初始化。

注: 构造方法 ——> @Autowired —— > @PostConstruct ——> 静态方法 (按此顺序加载)

使用此方法实现service调用有一个问题,若想对其他service进行调用,需将对应的service手动注入进来.

Num2:使用ApplicationContextAware
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

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

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 对应的被管理类有别名时使用
     * @param name
     * @param <T>
     * @return
     * @throws BeansException
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }
    
    /**
     * 在对应的注解内未使用别名时 使用
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

}

此方法直接实现了ApplicationContextAware接口,调用Spring提供的applicationContext,将所有在Spring中有注解的类,均可通过getBean方式获取到.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本来是准备看一看Spring源码的。然后在知乎上看到来一个帖子,说有一群**自己连Spring官方文档都没有完全读...
    此鱼不得水阅读 6,960评论 4 21
  • Spring致力于提供一种方法管理你的业务对象。在大量Java EE的应用中,随处可见Spring。今天我将简单的...
    JAVA架构师的圈子阅读 1,375评论 0 16
  • 概述 Spring是什么? Spring是一个开源框架,为了解决企业应用开发的复杂性而创建的,但是现在已经不止于企...
    琅筑阅读 1,230评论 2 8
  •   在我们写代码的时候,很多时候难免碰到一些需求是需要我们在项目启动的时候来 启动线程/读取缓存/开启某个服务等等...
    冯玉然阅读 11,009评论 0 4
  • 1,付出不亚于任何人的努力!你想不成功都难。 2,要谦虚不要骄傲! 3,要每天反省! 4,活着就要感谢!内求,利他...
    熊攸平阅读 1,161评论 0 0