实现InitializingBean接口,重写afterPropertiesSet方法
实现这个接口,Spring启动后,初始化Bean时,若该Bean实现InitialzingBean接口,会自动调用afterPropertiesSet()方法,完成自定义的初始化操作。
package cn.sk.com.conf;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SimpleModule;
import com.fasterxml.jackson.databind.ToStringSerializer;
import org.springframework.beans.factory.InitialzingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LongClassConverter implements InitialzingBean {
private final ObjectMapper objectMapper;
@Autowired
public LongClassConverter (ObjectMapper objectMapper){
this.objectMapper = objectMapper;
}
@Override
public void afterPropertiesSet(){
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class,ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
}
}