Spring问题总结

0

Spring注入的bean默认为单例,但是如果单例bean注入一个非单例的bean不能使用resource注入,而要是用个getBean

ContextLoader.getCurrentWebApplicationContext().getBean("beanName");

Spring仅单例的bean能执行销毁方法destroy-method

实现initdestory有三种方法:

  • 注解@PostConstruct@PreDestroy
  • xml配置init-methoddestory-method
  • bean实现InitializingBeanDisposableBean接口

注:destory好像不能对数据库数据进行操作,如果需要操作数据库最好使用监听器,而且测试最好不要用Eclipse配置的Tomcat测试。

错误java.lang.ClassCastException: com.sun.proxy.$Proxy51 cannot be cast to是因为getBean的时候要强转为接口。