註解@Repository的使用

@Repository用於標註數據訪問組件,即DAO組件;
例:

@Repository
public class VentorDaoImpl implements iVentorDao {

}
在一個稍大的項目中,如果組件採用xml的bean定義來配置,顯然會增加配置文件的體積,查找以及維護起來也不太方便。Spring2.5爲我們引入了組件自動掃描機制,他在類路徑下尋找標註了上述註解的類,並把這些類納入進spring容器中管理。

它的作用和在xml文件中使用bean節點配置組件時一樣的。要使用自動掃描機制,我們需要打開以下配置信息:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com.mstf">

</beans>

與其相似的還有以下註解:

@Service@Controller
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章