1、加载bean时,可以设置一个类初始化的方法和销毁bean时的方法。
<bean class="cn.com.dao.impl.UserDaoMySqlImpl" init-method="init" destroy-method="destroy"/>
2、使用spring的注解
@PostConstruct
public void init(){
System.out.println("userService init");
o.getUser();
}
@PreDestroy
public void destroy(){
System.out.println("userService destroy");
}
3、web.xml中配置
<listener>
<listener-class>com.epoch.customizeproject.scnt.servlet.SynUserServlet</listener-class>
</listener>
然后:实现接口 ServletContextListener重写contextInitialized方法
public void contextInitialized(ServletContextEvent event) {
WebApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
UserService userService = (UserService)applicationContext.getBean(UserService.class);
//这样就可以获取bean了
}