InitializingBean and DisposableBean callback interfaces
Aware interfaces for specific behavior
Custom init() and destroy() methods in bean configuration file
@PostConstruct and @PreDestroy annotations
Bean 생성 생명주기 콜백
@PostConsturct 어노테이션이 적용된 메소드 호출
Bean 이 InitializingBean 인터페이스 구현시 afterPropertiesSet호출
@Bean 어노테이션의 initMethod 에 설정한 메소드 호출
Bean 소멸 생성주기 콜백
@PreDestroy 어노테이션이 적용된 메서드 호출
Bean이 DisposableBean 인터페이스 구현시 destroy 호출
@Bean 어노테이션의 destroyMethod 에 설정한 메소드 호출
@Repository
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class MemoryVoucherRepository implements VoucherRepository, InitializingBean, DisposableBean {
private final Map<UUID, Voucher> storage = new ConcurrentHashMap<>();
@Override
public Optional<Voucher> findById(UUID voucherId) {
return Optional.ofNullable(storage.get(voucherId));
}
@Override
public Voucher insert(Voucher voucher) {
storage.put(voucher.getVoucherId(), voucher);
return voucher;
}
@PostConstruct
public void postConstruct(){
System.out.println("postConstruct Called!");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet called!");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy called!");
}
@PreDestroy
public void preDistroy(){
System.out.println("preDestroy called!");
}
}
@Configuration
public class AppConfiguration{
@Bean(initMethod = "init")
public BeanOne beanOne(){
return new BeanOne();
}
}
class BeanOne{
public void init(){
}
}