国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

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

2020-07-27 21:25:11
字體:
來源:轉載
供稿:網友

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的自動掃描功能以后,自動注入的功能也開啟了。

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤凰县| 云林县| 邢台县| 平果县| 旌德县| 桦南县| 博乐市| 横山县| 象州县| 资源县| 六安市| 乌兰察布市| 安徽省| 托克托县| 同德县| 婺源县| 遂宁市| 敦煌市| 西盟| 城市| 怀安县| 霍州市| 甘南县| 长沙市| 福建省| 临洮县| 湖南省| 景宁| 宁明县| 金坛市| 凯里市| 长丰县| 五指山市| 黄龙县| 章丘市| 平凉市| 三明市| 长岛县| 梁山县| 荆州市| 烟台市|