Spring Boot或Cloud的普通类如何获取service实现
我们在便利使用SpringBoot或SpringCloud的时候,很多时候会遇到一种场景,想在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方式获取到.
本文介绍在SpringBoot或SpringCloud环境下,如何在工具类中优雅地调用被Spring托管的service。通过两种方法实现:使用PostConstruct注解和实现ApplicationContextAware接口。前者通过实例化静态成员变量,后者则通过Spring的applicationContext获取bean。
1万+

被折叠的 条评论
为什么被折叠?



