Spring中的InitializingBean接口

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。

​package org.springframework.beans.factory;
public interface InitializingBean {
    void afterPropertiesSet() throws java.lang.Exception;
}

 例如一个service实现类中实现了该接口,就必须需要实现该afterPropertiesSet()方法

package com.imooc.service.impl;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import com.qiniu.util.StringMap;

@Service
public class UserServiceImpl implements IUserService, InitializingBean {

    private StringMap putPolicy;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
    }
}

在其它类中通过下述方式生成UserServiceImpl实例的时候都会执行该方法

@Autowired
private IUserService userService;

猜你喜欢

转载自blog.csdn.net/y_bccl27/article/details/114784369