【发布时间】:2013-08-25 04:51:33
【问题描述】:
Spring 提供了在 application-context.xml 文件中定义 bean 类和 DAO 类的功能,现在我正在定义这样的 bean 类
<context:component-scan base-package="com.forum.jsfbeans" />
但是在谷歌的很多地方我看到人们对 DAO、beans、服务类的定义不同,就像这样
<!-- Beans Declaration -->
<bean id="User" class="com.otv.model.User"/>
<!-- User Service Declaration -->
<bean id="UserService" class="com.otv.user.service.UserService">
<property name="userDAO" ref="UserDAO" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDAO" class="com.otv.user.dao.UserDAO">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
我们必须以不同的方式定义application-context.xml 文件中的所有类(就像我在上面所做的那样),或者我们可以只为每种类型的类使用下面的标签,而不是它的 DAO、Bean 或服务?
<context:component-scan base-package="com.forum.dao,com.forum.jsfbeans,com.forum.service" />
如果我们定义类似上面的东西,它就会起作用,并且 Spring 知道它必须在 DAO、Service 或 Bean 类中执行什么操作。
【问题讨论】:
-
你有什么问题?
-
@DavidWallace 我们必须以不同的方式定义 application-context.xml 文件中的所有类(就像我在上面所做的那样),或者我们可以只为每种类型的类使用下面的标签,而与其 DAO 无关, Bean 还是服务?