spring bean 实例化之后显式的指定调用自定义方法的几种方式

创建普通的类如下:

@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注解标注的方法

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值