阅读856 返回首页    go 阿里云 go 技术社区[云栖]


Spring中bean的初始化和销毁之前的方法XML和注解的两种形式


XML形式

package cn.xy;

public class StudentServiceImpl
{
 private StudentDao studentDao;

 public void init()
 {
  System.out.println("正在执行初始化的init方法");
 }

 public void save(Student s)
 {
  studentDao.insert(s);
 }
 
 
 public void destory()
 {
  System.out.println("正在执行销毁之前的destory方法");
 }

 public StudentDao getStudentDao()
 {
  return studentDao;
 }

 public void setStudentDao(StudentDao studentDao)
 {
  this.studentDao = studentDao;
 }

}

<bean />
<bean init-method="init" destory-method="destory">
 <property name="studentDao" ref="studentDao">
</bean>

 

 

注解形式

package cn.xy;

public class StudentServiceImpl
{
       
 private StudentDao studentDao;

 @PostConstruct
 public void init()
 {
  System.out.println("正在执行初始化的init方法");
 }

 public void save(Student s)
 {
  studentDao.insert(s);
 }
 
 @PreDestory
 public void destory()
 {
  System.out.println("正在执行销毁之前的destory方法");
 }

 public StudentDao getStudentDao()
 {
  return studentDao;
 }

 @Resource
 public void setStudentDao(StudentDao studentDao)
 {
  this.studentDao = studentDao;
 }

}

 

最后更新:2017-04-04 07:32:05

  上一篇:go jQuery中的$.extend方法来扩展JSON对象
  下一篇:go Servlet中的监听器及监听在线人数实例