spring註解方式自動注入接口的實現子類

applicationContext.xml配置文件加入:

 

<context:annotation-config/>

作用是隱式地向 Spring 容器註冊

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 這 4 個BeanPostProcessor。

註冊這4 BeanPostProcessor的作用,就是爲了你的系統能夠識別相應的註解。

 

 

    <!-- 設置需要進行Spring註解掃描的類包 -->

<context:component-scan base-package="xx.xx" /> 可以用*代表所有

@Service()

publicclass ServiceImpl implements Service

調用時:

    @Resource()

private Service Service;

 

這樣的話就能實現自動注入了

@Service一般用來定義Service daoaction都有對應的註解網上可查

3種上面還有一種通用的不好定義組建類別時可用

發佈了23 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章