1. 程式人生 > >spring自動檢測bean

spring自動檢測bean

在上一篇《spring定義和裝載bean》中,最後提到了通過配置<context:annotation-config/>,從而使spring可以使用註解@Autowired的方式自動裝配依賴。所謂的依賴,就是在xml配置檔案中定義的bean。在xml檔案中定義bean也是一件很討厭的事情:如果java類很多的話,需要定義很久,並且新增的類經常忘記加到spring配置檔案中。本篇部落格就來討論如何使用註解來定義bean,從而讓spring檢測到並註冊到spring容器中。

加入component-scan配置

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

在spring的配置檔案中,加入了<context:component-scan base-package="kite.springProj1"/>,並指定了base-package,spring在啟動時就會掃描base-package下(及其sub packages下)的所有類,當發現符合條件的類,就會自動註冊成spring bean。

標註bean

為了配合spring的component scan,我們需要標註出哪些是component。具體來講,當我們把以下幾個註解中的任何一個新增到一個類上面時,spring掃描到這個類時就會將其註冊為spring bean。以下是可用的註解

  • @Component
  • @Controller
  • @Repository
  • @Service

如果需要標註的java類沒有什麼特殊含義,就用@Component就行。其餘幾個註解除了將該類註冊為spring bean以外,還有別的含義。

示例

介面類

package kite.springProj1;

public interface Car {
    void go();
}

其中一個實現類

package kite.springProj1;

import org.springframework.stereotype.Component;

@Component
public class BMW implements Car {
    public void go() {
        System.out.println("bmw go......");
    }
}

下面這個類依賴上面的介面

package kite.springProj1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Person {

    Car car = null;
    public Car getCar() {
        return car;
    }

    @Autowired
    public void setCar(Car car) {
        this.car = car;
    }

    public void Drive(){
        car.go();
    }
}

下面是main方法

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Person tom = (Person) context.getBean("person");
        tom.Drive();
    }
}

最上面的xml配置檔案就是bean.xml檔案的全部內容。

執行上面的main方法,會建立一個spring容器,並載入上面的兩個component,分別是bean的名字會是bmw和person,bmw以byType的方式被自動裝配到person中,而person會被建立並複製給tom變數。輸出結果是

bmw go......

相關推薦

Spring自動檢測Bean——Componet-Scan

一、自動檢測 自動檢測(autodiscovery) 讓Spring能夠自動識別哪些類需要被裝配成SpringBean 從而減少在xml配置檔案對 元素的使用。 注:自動檢測和是否使用註解無關,它並不侷限應用於註解方式的程式設計(包括註解自動裝配bean以及註解註冊bean)

spring自動檢測bean

在上一篇《spring定義和裝載bean》中,最後提到了通過配置<context:annotation-config/>,從而使spring可以使用註解@Autowired的方式自動裝配依賴。所謂的依賴,就是在xml配置檔案中定義的bean。在xml

Spring自動裝配Bean詳解

att spa bject 快速 個數 就會 否則 strong pro 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin

Spring自動裝配Bean——xml方式與註解方式

自動裝配的概念 在Spring使用中,我們在xml配置檔案通過元素或元素的ref屬性向bean注入另外的依賴bean。 如果使用自動裝配(autowiring) ,就可以減少甚至消除配置元素和元素。 設定元素的autowire屬性就可以設定bean的自動裝配模式。自動裝配有5種模式。

spring 自動裝配bean

no – 預設情況下,自動配置是通過“ref”屬性手動設定 <bean id="customer" class="com.yiibai.common.Customer" autowire=""> <property name="person" ref="person" />

Spring Bean自動檢測

  1-自動檢測bean 需要用到<context:component-scan> 注意:a) 需要include進來xmlns:context名稱空間;base-package指的是我們要掃描這個包下所有的內容    2-新增過濾器,自定義掃描 &l

3.spring自動裝配/Bean之間的關係/作用域/外部檔案/spel/

1.自動裝配/手動裝配 xml配置檔案裡的bean自動裝配 Spring IOC 容器裡可以自動的裝配Bean,需要做的僅僅是在<bean>的autowire屬性裡面指定自動裝配模式 ->byType(根據型別自動進行裝配):若IOC容器裡需要有多個與目標Bean型別一樣的Bean,在這種

3.spring自動裝配/Bean之間的關系/作用域/外部文件/spel/

depends ... 完全 構造器 外部文件 conn wire odi host 1.自動裝配/手動裝配 xml配置文件裏的bean自動裝配 Spring IOC 容器裏可以自動的裝配Bean,需要做的僅僅是在<bean>的autowire屬性裏面指定自動裝

Spring學習(穀粒學院spring4課程)第五節 基於 @Autowired 自動裝配 Bean

1: @Autowired 的應用範圍  @Autowired 可以應用到構造器,欄位,含參函式上。 2:如何裝配 2.1 @Autowired 自動裝配具有相容型別的單個 Bean屬性,若該屬性非必須,可以設定required 屬性為 false 2.2若有多個be

Spring自動裝配Bean的屬性

Spring的最基本的能力就是DI,即依賴注入,或控制反轉,它可以為Bean注入其依賴的其他Bean。 一個Bean依賴其他Bean一般是通過在Bean中定義其他Bean的成員變數的方式來實現的,那麼,Spring的DI也就表現為給Bean的屬性自動注入值。 這一般分為以下其中情況。 1

Spring Boot無法自動注入bean問題解決方案

前言:在使用Spring Boot+Mybatis時,寫完Mapper介面、Service、Controller後,啟動(圖1)打包專案(圖2)均失敗,提示無法自動注入bean。完成目標:1、無法自動注入bean錯誤資訊2、程式碼展示3、解決方案一、錯誤資訊:Descript

Spring學習】spring註解自動注入bean

Spring mvc註解用到的配置: <!-- 啟用spring mvc 註解 --> <context:annotation-config /> <context:component-scan base-package

關於spring boot無法自動注入bean問題解決方案

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_|

spring基礎知識 (15):使用註解自動裝配bean

一般情況下,控制層(controller)需要呼叫業務層(service)進行業務邏輯處理,業務層呼叫持久層(dao)訪問資料庫。這時候就需要使用自動裝配注入相應的bean.在controller中注入service,在service中注入reposito

spring專案中quartz的job類不能自動注入bean的問題

在execute()方法中加入以下紅色部分程式碼://定時清除失效商品public class ClearInvalidGoodsJob implements Job {@Autowiredprivate GoodsInfoService goodsInfoservice;@

解決Spring中Quart無法自動注入Bean問題

        因專案需求,之前專案中已經有定時器的例子了,但是需求那邊過來的需求之前用的定時器並不能滿足,之前的定時器是用spring裡面的@Scheduled(cron = "0 0 0 * * ? ")這種方式,這是一種死的定時器,需求要的是動態定時器,使用者新增一個

spring boot無法自動注入bean?原因在這兒

Description: Field helloSpringBootService in com.zd.hellospringboot.HelloController required a bean of type 'com.zd.service.HelloSpringBo

Spring自動裝配Bean的四種方式

Spring提供了四種自動裝配的方式(Version 5.0.6.RELEASE),分別是: no:(預設)無自動裝配。 Bean引用必須通過ref元素定義。 byName:自動按屬性名稱進行呼叫。

spring mvc 從bean自動獲取物件

寫在前面 在日常開發或者學習spring mvc web專案中,當專案打包部署後,某些自定義配置項引數可能會需要改動。而這些配置項引數如果被寫死進專案中,或許會造成很大的麻煩。因此,有時候這些需要變動的自定義的配置項引數,便需要寫進配置檔案中。spring m

Spring學習(2)--在xml中使用autowire自動裝配Bean屬性

spring的自動裝配:是指對於當前需要裝配的bean的屬性,不用使用手工方式顯示裝配,而是讓spring自己通過在IOC容器中按照一定的規則查詢相關的符合條件的bean,裝配為bean的屬性。 <bean>的autowire屬性有六個值,說明如下: 1、