使用 Bean來匯入第三方元件

2021-08-19 17:01:36 字數 1755 閱讀 6969

首先建立乙個配置類(相當於spring的配置檔案),該類上需要加上@configuration註解來告訴spring這個是配置類,

再加上@componentscan(value=「包名」)來告訴spring要掃瞄哪些包下的類。

在該配置類下寫乙個方法,在該方法上加上@bean(value=「名字」),就可以將該bean註冊到ioc容器中,

該方法的返回值就是該bean的型別,不加value時,該方法名就是該bean的名字,加value時,value的值為該bean的名字。

**如下:

package com.forever.config;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.componentscan;

import org.springframework.context.annotation.componentscan.filter;

import org.springframework.context.annotation.componentscans;

import org.springframework.context.annotation.configuration;

import org.springframework.context.annotation.filtertype;

import org.springframework.stereotype.controller;

import com.forever.bean.person;

@configuration //(告訴spring這是乙個配置類)

@componentscan(value="com.forever") //要掃瞄的包

public class mainconfig

}

該註冊方法可以在@componentscan中加入過濾器,可以選擇哪些bean需要載入,或者哪些bean不需要載入。如果過濾器type定義為filtertype.custom時,需要自己寫乙個過濾器實現typefilter介面並重寫match方法來執行自己的過濾規則。並將預設的過濾規則設定為false,即userdefaultfilters=false

@componentscan(value="com.forever",includefilters=)

},usedefaultfilters=false)

})//@componentscan value指定要掃瞄的包名

//excludefilters = filter,指定掃瞄的時候按照什麼規則排除哪些元件

//includefilters = filter,指定掃瞄的時候包含哪些元件

按照註解

按照給定的型別

使用aspectj表示式

使用正規表示式

使用自定義規則

//filtertype.

自定義過濾器的**如下:

public class mytypefilter implements typefilter 

return false; //否則不載入

}

也可以使用@componentscans()執行多個過濾規則。

測試**(在junit中進行測試):

public void test3()

}

匯入第三方細節

最近匯入第三方的東西比較多 fmdb 第三方連線sqlite資料庫的用的,匯入的框架 libsqlite3.0tbd ios9.2 版本不同,字尾不同,fmdb不支援arc記得 選中專案 build phases 選中第二項,尋找相關的檔案在後面跟上 fno objc arc欄位 masnory 第...

使用CMake匯入第三方庫

使用cmakelists.txt 中間需要匯入第三方的庫當我們需要生成自己的庫的時候,下面介紹如何匯入第三方庫的具體步驟。cmake的配置資訊寫在了cmakelist.txt檔案中。在cmakelist.txt檔案裡,我們首先定義兩個變數inc dir和link dir,用來表示標頭檔案路徑和庫的路...

使用CocoaPod匯入Swift第三方庫報錯

在使用cocoapod匯入swift第三方時出現這樣的錯誤 pods written in swift can only be integrated as frameworks add use frameworks to your podfile or target to opt into usin...