濮阳杆衣贸易有限公司

主頁 > 知識庫 > Spring組件自動掃描詳解及實(shí)例代碼

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

熱門標(biāo)簽:南昌市地圖標(biāo)注app 地圖標(biāo)注市場怎么樣 企業(yè)怎么在聯(lián)通申請400電話 百度地圖添加標(biāo)注圖標(biāo)樣式 泰州泰興400電話 怎么申請 聊城智能電銷機(jī)器人外呼 好操作的電話機(jī)器人廠家 如何用中國地圖標(biāo)注數(shù)字點(diǎn) 南京新思維電話機(jī)器人

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

問題描述

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

解決方案

Spring 提供組件掃描(component scanning)功能。它能從classpath里自動掃描、偵測和實(shí)例化具有特定注解的組件。基本的注解是@Component,它標(biāo)識一個(gè)受Spring管理的組件。其他特定的注解有@Repository、@Service和@Controller,它們分別標(biāo)識了持久層、服務(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的自動掃描功能以后,自動注入的功能也開啟了。

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

您可能感興趣的文章:
  • Spring MVC 注解自動掃描失效原因分析
  • Spring自動掃描無法掃描jar包中bean的解決方法

標(biāo)簽:銅川 自貢 白銀 開封 吉林 臨汾 烏蘭察布 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Spring組件自動掃描詳解及實(shí)例代碼》,本文關(guān)鍵詞  Spring,組件,自動,掃描,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Spring組件自動掃描詳解及實(shí)例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于Spring組件自動掃描詳解及實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    富宁县| 剑河县| 梅州市| 乌恰县| 绥德县| 安岳县| 庐江县| 塘沽区| 美姑县| 慈溪市| 民乐县| 济宁市| 巴林右旗| 永登县| 鲁山县| 河西区| 盐城市| 韶山市| 商洛市| 高尔夫| 青浦区| 庆云县| 昌都县| 遂溪县| 梁山县| 新密市| 武宣县| 沾化县| 红安县| 进贤县| 屏边| 且末县| 惠水县| 叙永县| 永州市| 休宁县| 民和| 东丰县| 南汇区| 旌德县| 城口县|