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;
本文介绍了Spring框架中的InitializingBean接口及其应用场景。该接口提供了一个afterPropertiesSet方法,用于bean的初始化操作。通过示例展示了如何在UserServiceImpl类中实现此接口,并在bean创建时调用该方法。
1230





