背景:@Scope是默认时, @PreDestroy仍然不执行
@Data
@NoArgsConstructor
@Component("stu")
@Primary
public class StudentImpl implements Serializable, Student {
//······
@PreDestroy
private void des() {
System.out.println("stu PreDestroy---------------------------------");
}
}
@PreDestroy
private void des() {
System.out.println("stu PreDestroy---------------------------------");
}
@Scope
默认是单例模式,单例模式的对象是不会自动销毁的,因为一直有引用链,不会被GC。改为@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
应该就会生效了。
bean消亡时才会执行