【问题标题】:Declaration of beans and Dao classes in SpringSpring中bean和Dao类的声明
【发布时间】: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 还是服务?

标签: java spring


【解决方案1】:

这只是实现相同结果的两种不同方法。有些人喜欢在 xml 中定义 bean,而另一些人则喜欢使用组件扫描进行自动检测。

【讨论】:

  • 如果我们使用 'component-scan' 会影响性能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2015-07-03
  • 2011-05-07
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
相关资源
最近更新 更多