注解一个DAO类作为一个bean,即使有实现类,容器并不能创建该接口的作为Bean.
bean类需要一个无参构造方法,显然接口是没有的.
注解传入的只是一个接口,其实现类容器开始是不能获知的.所以,不能想当然地觉得容器能用实现类的构造方法.
在spring-mvc.xml文件中,声明一个接口
<bean id="userDao" class="com.loop.dao.UserDao" abstract="true"/> //需要声明这是个抽象类.
Is this bean "abstract", that is, not meant to be instantiated itself but rather just serving as parent for concrete child bean definitions?
The default is "false". Specify "true" to tell the bean factory to not try to instantiate that particular bean in any case.
可以看到,abstract 为true的类只是作为其他bean的父类.
本文探讨了Spring框架中如何正确配置DAO接口作为Bean,并解析了抽象Bean的概念及应用。通过实例说明了为什么直接注解DAO接口无法让Spring容器实例化,以及如何通过配置抽象Bean来解决这一问题。
542

被折叠的 条评论
为什么被折叠?



