Spring问题总结
0
Spring
注入的bean
默认为单例,但是如果单例bean
注入一个非单例的bean
不能使用resource
注入,而要是用个getBean
:
ContextLoader.getCurrentWebApplicationContext().getBean("beanName");
Spring
仅单例的bean
能执行销毁方法destroy-method
。
实现init
和destory
有三种方法:
- 注解
@PostConstruct
和@PreDestroy
xml
配置init-method
和destory-method
bean
实现InitializingBean
和DisposableBean
接口
注:
destory
好像不能对数据库数据进行操作,如果需要操作数据库最好使用监听器,而且测试最好不要用Eclipse配置的Tomcat测试。
错误java.lang.ClassCastException: com.sun.proxy.$Proxy51 cannot be cast to
是因为getBean
的时候要强转为接口。