基于Spring IOC容器实现工具类静态方法调用的三种方式

背景说明
在spring MVC 的项目中定义配置读取工具类时,发现需要使用Spring IOC容器进行注入,原始方法就是直接使用@Autowired 将依赖类注入到工具类中,想要用工具类时也得需要借助@Autowired进行注入,操作起来太复杂,完全丧失了工具类的灵活性。

下面整理了将Spring IOS容器中的对象注入到工具类静态方法中的三种实现方式,详细方式如下所示:
1、基于@PostConstruct 实现代码,详细代码如下所示:

//服务
import org.springframework.stereotype.Service;
@Service
public class ConfigService {
    public String get(String key){
        return "OK";
    }
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class ConfigUtil {
    private static ConfigService configService;
    //IOC注入
    @Autowired
    @Qualifier("configService")
    ConfigService iocConfigService;
    //初始化赋值
    @PostConstruct
    public void init() {
        this.configService = iocConfigService;
    }
    //静态方法
    public static String  get(String key){
        return configService.get(key);
    }
}  

2、基于set方法实现代码,详细代码如下所示:

//配置服务
import org.springframework.stereotype.Service;
@Service
public class ConfigService {
    public String get(String key){
        return "OK";
    }
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ConfigUtil {
	//注入服务
    private static ConfigService configService;
    //服务赋值
    @Autowired
    public void setConfigService(ConfigService configService) {
        ConfigUtil.configService = configService;
    }
    //静态方法
    public static String  get(String key){
        return configService.get(key);
    }
}

  注:此Set方法之上需加上 @Autowired 注解
3、基于default-autowire="byName" 实现代码,详细代码如下所示:

//配置服务
import org.springframework.stereotype.Service;
@Service
public class ConfigService {
    public String get(String key){
        return "OK";
    }
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ConfigUtil {
	
    private static ConfigService configService;
    //赋值服务方法
    public void setConfigService(ConfigService configService) {
        ConfigUtil.configService = configService;
    }
	//获取服务方法
    public static String  get(String key){
        return configService.get(key);
    }
}

注:此种方法中需要使用 @Autowired 注解,但是需要在spring 的配置xml 中加上default-autowire="byName" 配置,详细代码如下所示:

<!--spring.xml-->  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      "  default-autowire="byName">
	  //...........
	  //...........
</beans>

以上代码均可实现将spring IOC容器中代理类,注入到工具类静态方法中,可根据个人习惯选择使用

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值