废话不多说:
@Component
public class ESUtil {
private static JestClient client;
@Autowired
private JestClient jestClient;
@PostConstruct
public void init(){
client = this.jestClient;
}
public static boolean indexExists(String indexName) throws IOException {
return client.execute(new IndicesExists.Builder(indexName).build()).isSucceeded();
}
}
①把当前工具类声明为spring的组件(component)
②注入一个对象
③静态初始化一个需要注入的对象
④在init方法上使用@PostConstruct注解,这样util类在初始化后init方法就会被spring容器执行,进行属性的装配,也就是注入。
⑤使用注入的对象进行操作