创建普通的类如下:
@Data
public class User {
private Student student;
@Autowired
public void student(Student student) {
System.out.println("User...为student赋值");
this.student = student;
}
public User() {
System.out.println("User...无参构造");
}
public void myInit() {
System.out.println("User...myInit()");
}
public void myDestroy() {
System.out.println("User...myDestroy()");
}
}
@Data
@Component
public class Student {
private String sName;
}
第一种:
//使用@Bean注解指定
@Configuration
@ComponentScan("com.example.demo.pojo")
public class MyConfig {
@Bean(initMethod = "myInit",destroyMethod = "myDestroy")
public User user() {
return new User();
}
}
测试:
public class MainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
context.close();
}
}
结果:
User...无参构造 //第一步 调用无参构造实例化User
User...为student赋值 //第二步 为属性赋值
User...myInit() //第三步 调用指定的初始化方法
User...myDestroy() //第四步 容器关闭的时候执行指定的销毁的方法
----------------------------------------------------------------------------------------------------
第二种:
实现 InitializingBean , DisposableBean 接口,重写里面的
afterPropertiesSet和destroy 方法
@Data
public class User implements InitializingBean , DisposableBean {
private Student student;
@Autowired
public void student(Student student) {
System.out.println("User...为student赋值");
this.student = student;
}
public User() {
System.out.println("User...无参构造");
}
@Override
public void destroy() throws Exception {
System.out.println("User...destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("User...afterPropertiesSet");
}
}
@Configuration
@ComponentScan("com.example.demo.pojo")
public class MyConfig {
@Bean
public User user() {
return new User();
}
}
测试:
public class MainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
context.close();
}
}
结果:
User...无参构造 第一步 实例化User
User...为student赋值 第二步 为属性赋值
User...afterPropertiesSet 第三步 调用重写的afterPropertiesSet 方法,看名字可以知道是在属性赋值之后执行该方法
User...destroy 第四步 容器关闭的时候调用销毁方法
该接口解释的很清楚, 在所有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;
}
在销毁bean 的时候执行
public interface DisposableBean {
/**
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
* @throws Exception in case of shutdown errors. Exceptions will get logged
* but not rethrown to allow other beans to release their resources as well.
*/
void destroy() throws Exception;
}
----------------------------------------------------------------------------------------------------
第三种:
//使用@PostConstruct 和@PreDestroy 注解
@Data
public class User {
private Student student;
@Autowired
public void student(Student student) {
System.out.println("User...为student赋值");
this.student = student;
}
public User() {
System.out.println("User...无参构造");
}
@PostConstruct
public void init() throws Exception {
System.out.println("User...init");
}
@PreDestroy
public void destroy() throws Exception {
System.out.println("User...destroy");
}
}
测试:
public class MainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
context.close();
}
}
结果:
User...无参构造 //第一步 实例化User
User...为student赋值 //第二步 赋值
User...init //第三步 调用 @PostConstruct 注解标注的方法
User...destroy //第四步 bean 销毁调用@PreDestroy注解标注的方法