Spring-框架-bean生命周期回调方法注解

生命周日回调方法:

 bean的生命周期:

           init():初始化方法

           destory():销毁方法

对应的注解:

@PostConstruct:注解用在方法上,用于bean初始化之后的行为方法

init(){}

@PreDestroy:注解用在消费方法上面,在bean消费之前调用该注解方法

destroy(){}

代码:

package com.spring.annotationDI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: StudentService
 * @Description: TODO
 **/
@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    public int save() {
        System.out.println("基于注解的service层的调用。。。");
        return studentDao.save();
    }
}

测试:

package com.spring.annotationDI;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @ClassName: Test
 * @Description: TODO
 **/

public class Test {
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
        context.registerShutdownHook();
        StudentService bean = context.getBean(StudentService.class);
        bean.save();

    }
}

结果:

Spring-框架-bean生命周期回调方法注解_第1张图片

 

你可能感兴趣的:(后端)