在工具类中静态方法使用Spring的组件

版权声明: https://blog.csdn.net/Dongguabai/article/details/83275790
/**
 * @author Dongguabai
 * @date 2018/9/27 10:14
 */
@Component
public class VwOrgSupport {

    /**
     * 可显示最低级别orgType
     */
    private static final String LOWEST_ORGTYPE_05 = "05";
    private static final String LOWEST_ORGTYPE_06 = "06";

    /**
     * N-表 获取地区划分x 轴地区信息
     * @see VwOrgQuery
     * @param orgId   用户选择的区域的orgNo
     * @return  当前柱状图x 轴
     */
    public static List<VwOrgQuery> getVwOrgQueryByPOrgNoAndLowOrgType(String orgId){
        String orgType = vwOrgSupport.vwOrgMapper.getOrgTypeByOrgNo(orgId);
        if (LOWEST_ORGTYPE_05.equals(orgType)){
            orgId = orgId.substring(0,orgId.length()-2);
            return vwOrgSupport.vwOrgMapper.getVwOrgQueryByPOrgNoAndLowOrgType(orgType,orgId);
        }
        if (LOWEST_ORGTYPE_06.equals(orgType)){
            orgType = LOWEST_ORGTYPE_05;
            orgId = orgId.substring(0,orgId.length()-4);
            return vwOrgSupport.vwOrgMapper.getVwOrgQueryByPOrgNoAndLowOrgType(orgType,orgId);
        }
        orgType = "0"+(Integer.valueOf(orgType)+1);
        return vwOrgSupport.vwOrgMapper.getVwOrgQueryByPOrgNoAndLowOrgType(orgType,orgId);
    }

    @Autowired
    private VwOrgMapper vwOrgMapper;

    private VwOrgSupport(){}

    private static VwOrgSupport vwOrgSupport;

    @PostConstruct
    public void init() {
        vwOrgSupport = this;
        vwOrgSupport.vwOrgMapper = this.vwOrgMapper;
    }
}

主要是使用了@PostConstruct注解,这个注解会在Servlet容器初始化的时候执行,具体执行时机是在构造函数之后,init()方法之前执行。还有一个@PreDestroy注解,被这个注解修饰的方法在destroy()方法执行执行之后执行。

还有一种方式是可以使用Spring工具类(参看:https://blog.csdn.net/Dongguabai/article/details/80788646)。

猜你喜欢

转载自blog.csdn.net/Dongguabai/article/details/83275790