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;
}
}
本文转自IT徐胖子的专栏博客51CTO博客,原文链接http://blog.51cto.com/woshixy/982994如需转载请自行联系原作者
woshixuye111