美文网首页springboot
Spring Boot或Cloud的普通类如何获取service

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

作者: YannisChen_2017 | 来源:发表于2019-06-13 14:52 被阅读11次
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 Boot或Cloud的普通类如何获取service

    本文链接:https://www.haomeiwen.com/subject/pslsfctx.html