工具类中注入service及静态方法中调用非静态方法

一:静态方法中调用非静态方法

在静态方法a中调用静态方法b,b的参数为该类的对象,如

static void sendSmsJfForStatic(ChuangLanSendSmsUtil chuangLanSendSmsUtil){
        chuangLanSendSmsUtil.sendSmsJf();
}

再在静态方法b中调用非静态方法。

工具类中注入service及静态方法中调用非静态方法

工具类中注入service及静态方法中调用非静态方法 

工具类中注入service及静态方法中调用非静态方法

注意:此处做了处理,直接将 sendSmsJf方法 设置为静态方法。直接可被静态方法a调用。

 

二:工具类用注入service

在spring-mvc中配置扫描,扫描该工具类

<context:component-scan base-package="工具类路径" />

工具类中注入service及静态方法中调用非静态方法

在工具类加上component注解,并实现ApplicationContextAware

工具类中注入service及静态方法中调用非静态方法

定义一个applicationContext,在静态方法中使用就设置静态。直接通过getBean方法获取bean 。

工具类中注入service及静态方法中调用非静态方法

参考链接:https://blog.csdn.net/junchenbb0430/article/details/78606380