好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

Spring組件自動掃描詳解及實例代碼

熱門標簽:南昌市地圖標注app 地圖標注市場怎么樣 企業怎么在聯通申請400電話 百度地圖添加標注圖標樣式 泰州泰興400電話 怎么申請 聊城智能電銷機器人外呼 好操作的電話機器人廠家 如何用中國地圖標注數字點 南京新思維電話機器人

Spring組件自動掃描詳解及實例代碼

問題描述

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

解決方案

Spring 提供組件掃描(component scanning)功能。它能從classpath里自動掃描、偵測和實例化具有特定注解的組件。基本的注解是@Component,它標識一個受Spring管理的組件。其他特定的注解有@Repository、@Service和@Controller,它們分別標識了持久層、服務處和表現層的組件。

實現方法

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> 

注意:當開啟Spring的自動掃描功能以后,自動注入的功能也開啟了。

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

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

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

巨人網絡通訊聲明:本文標題《Spring組件自動掃描詳解及實例代碼》,本文關鍵詞  Spring,組件,自動,掃描,詳解,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Spring組件自動掃描詳解及實例代碼》相關的同類信息!
  • 本頁收集關于Spring組件自動掃描詳解及實例代碼的相關信息資訊供網民參考!
  • 推薦文章