spring 中自定义工具类 解决@Autowired为null

注解方式

@Component
public class TimerUtil {

    @Autowired
    private DeptConfigService deptConfigService;

    public static  TimerUtil timerUtil;

    @PostConstruct
    public void init(){
        timerUtil = this;
    }
    
    //使用timerUtil.deptConfigService.xxx 调用即可
    public static void test(){
        List<String> deptIdList = timerUtil.deptConfigService.getDeptIdByIfDelNotice();
        System.out.println(deptIdList);
    }
   
}

xml配置方式

在spring-comtext.xml中配置以下bean就好了

<bean id="testUtils" class="这里写utils类的包全路径名" init-method="init"></bean>
posted @ 2021-11-29 15:14  GKGKGK  阅读(159)  评论(0编辑  收藏  举报