工具类注入Service

来源:互联网 发布:子域名是否需要备案 编辑:程序博客网 时间:2024/05/23 18:31

工具类使用注解注入Service

静态工具类注入

1、在spring自动扫描包设置中将工具类所在的包加进来
代码:

@Componentpublic class TestUtil {    @Resource    private TEStationService stationService;    private static TestUtil testUtil;    @PostConstruct    public void init() {        testUtil = this;    }    public static void addAdmin(String phone, String openid,int role) {        switch (role) {        case 1:            System.out.println("hahahahhahahh");            testUtil.stationService.updateAdmin(phone, openid);            break;        default:            break;        }    }}

非静态工具类注入

@Componentpublic class Test1 {    @Resource    private TEStationService stationService;    private static Test1 testUtil;    @PostConstruct    public void init() {        testUtil = this;    }    public void addAdmin() {            System.out.println("hahahahhahahh");            testUtil.stationService.updateAdmin("123123", "qweqwe");    }}
阅读全文
0 0
原创粉丝点击