1. 初始化Bean
~~~
public interface InitializingBean {
/**
* Invoked by the containing {@code BeanFactory} after it has set all bean properties
* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
* <p>This method allows the bean instance to perform validation of its overall
* configuration and final initialization when all bean properties have been set.
* @throws Exception in the event of misconfiguration (such as failure to set an
* essential property) or if initialization fails for any other reason
*/
void afterPropertiesSet() throws Exception;
}
~~~
1. 在factoryBean設置了它的所有屬性值后,Bean可以實現的接口,**用來執行自定義初始化**,**也可以用來檢查規定的屬性值是否被賦值**。
2. 只有一個afterPropertiesSet方法,完成以上動作
3. 不能通過IOC注入的屬性,例如只能通過new來創建的對象
~~~
public class XxlJobAdminConfig implements InitializingBean, DisposableBean {
@Value("${xxl.job.triggerpool.fast.max}")
private int triggerPoolFastMax;
@Resource
private XxlJobInfoDao xxlJobInfoDao;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(".....triggerPoolFastMax::" + triggerPoolFastMax);
System.out.println(".....XxlJobInfoDao::" + xxlJobInfoDao);
adminConfig = this;
xxlJobScheduler = new XxlJobScheduler();
xxlJobScheduler.init();
}
~~~
如下,在所有Bean的properties(由factory)被賦值后,執行afterPropertiesSet
```
.....triggerPoolFastMax::200
.....XxlJobInfoDao::org.apache.ibatis.binding.MapperProxy@56a4abd0
```