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 id="studentDao" class="cn.xy.StudentDao" />
<bean id="studentService" class="cn.xy.StudentServiceImpl" 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;
 }

}