最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring組件自動(dòng)掃描詳解及實(shí)例代碼

 更新時(shí)間:2017年02月09日 14:53:18   投稿:lqh  
這篇文章主要介紹了Spring組件自動(dòng)掃描詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

Spring組件自動(dòng)掃描詳解及實(shí)例代碼

問題描述

一個(gè)系統(tǒng)往往有成千上萬的組件,如果需要手動(dòng)將所有組件都納入spring容器中管理,是一個(gè)浩大的工程。

解決方案

Spring 提供組件掃描(component scanning)功能。它能從classpath里自動(dòng)掃描、偵測(cè)和實(shí)例化具有特定注解的組件?;镜淖⒔馐茾Component,它標(biāo)識(shí)一個(gè)受Spring管理的組件。其他特定的注解有@Repository、@Service和@Controller,它們分別標(biāo)識(shí)了持久層、服務(wù)處和表現(xiàn)層的組件。

實(shí)現(xiàn)方法

User.Java

package com.zzj.bean; 
 
import javax.annotation.Resource; 
 
import org.springframework.stereotype.Component; 
@Component 
public class User { 
  @Resource 
  private Car car; 
 
  public void startCar(){ 
    car.start(); 
  } 
} 

Car.java

package com.zzj.bean; 
 
import org.springframework.stereotype.Component; 
 
@Component 
public class Car { 
  public void start(){ 
    System.out.println("starting car..."); 
  } 
} 

XML配置文件

<?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.xsd 
      http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context.xsd"> 
      
    <context:component-scan base-package="com.zzj.bean"/> 
</beans> 

注意:當(dāng)開啟Spring的自動(dòng)掃描功能以后,自動(dòng)注入的功能也開啟了。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

木兰县| 青阳县| 玉屏| 年辖:市辖区| 嘉荫县| 沙河市| 长沙市| 黄平县| 洞口县| 都匀市| 类乌齐县| 临潭县| 聊城市| 惠东县| 赤城县| 交城县| 岳西县| 海晏县| 平塘县| 和硕县| 西吉县| 吴忠市| 山东省| 旺苍县| 临武县| 偃师市| 西充县| 瑞安市| 吴忠市| 横峰县| 怀仁县| 泽普县| 莒南县| 蚌埠市| 上栗县| 玉田县| 永州市| 额敏县| 中西区| 习水县| 合山市|