久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring IOC裝配Bean過(guò)程解析

瀏覽:139日期:2023-09-08 18:08:14

Spring的依賴(lài)注入

Spring主要支持兩種依賴(lài)注入方式,分別是屬性注入和構(gòu)造函數(shù)注入。同時(shí)也支持工廠(chǎng)方法注入方式。

屬性注入

屬性注入的方式非常簡(jiǎn)單,即指通過(guò)setXxx()方法注入Bean的屬性值或依賴(lài)對(duì)象。如下實(shí)例

編寫(xiě)User類(lèi)

public class User { private String username; private String address; public User() { } public User(String username, String address) { this.username = username; this.address = address; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return 'User{' +'username=’' + username + ’’’ +', address=’' + address + ’’’ +’}’; }}

編寫(xiě)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:p='http://www.springframework.org/schema/p' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd'> <bean class='com.rookie.bigdata.domain.User'> <property name='username' value='張三'></property> <property name='address' value='北京'></property> </bean></beans>

測(cè)試

ApplicationContext applicationContext=new ClassPathXmlApplicationContext('injection/bean.xml');

User user=(User)applicationContext.getBean('user');System.out.println(user);

構(gòu)造函數(shù)注入

構(gòu)造函數(shù)注入是屬性注入的另一種常用的注入方式。

xml配置方式如下

<bean class='com.rookie.bigdata.domain.User'> <constructor-arg name='username' value='李四'></constructor-arg> <constructor-arg name='address' value='上海'></constructor-arg></bean>

注入?yún)?shù)

XML中含有5個(gè)特殊符號(hào),分別是&,<,>,”,‘,如果配置文件中的注入值包含這些特殊的字符,就需要進(jìn)行特殊的處理。有兩種解決方法,其一:采用特殊標(biāo)簽,將包含特殊字符的字符串封裝起來(lái)。其二:使用XML轉(zhuǎn)義序列表示這些特殊字符。

<bean class='com.rookie.bigdata.domain.User'> <property name='username' value='張三'></property> <!-- <property name='address' value='北京'></property>--> <property name='address'> <value><![CDATA[北京&通州]]></value> </property></bean>

特殊字符 轉(zhuǎn)義序列 特殊字符 轉(zhuǎn)義序列 < &lt; “ &quot; > &gt; ’ &apos; & &amp;

基于注解的配置

<context:component-scan base-package='com.rookie.bigdata.annotation'></context:component-scan>

component-scan 的base-package屬性指定一個(gè)需要掃描的基類(lèi)包,Spring容器會(huì)掃描這個(gè)基類(lèi)包里面的所有的屬性,并從類(lèi)的注解信息中獲取Bean的定義信息。

如果想掃描特定的類(lèi),可以使用resource-pattern屬性過(guò)濾出特定的類(lèi)。如:

<context:component-scan base-package='com.rookie.bigdata' resource-pattern='annotation/*.class'></context:component-scan>

即Spring僅會(huì)掃描基類(lèi)包里annotation子包中的類(lèi)。

通過(guò)使用resource-pattern發(fā)現(xiàn),還是有很多時(shí)候并不滿(mǎn)足要求,此時(shí)可以通過(guò)過(guò)濾表達(dá)式。如下:

類(lèi)別 示例 說(shuō)明 annotation com.rookie.bigdata.XxxAnnotation 所有標(biāo)注了XxxAnnotation的類(lèi),該類(lèi)型采用目標(biāo)類(lèi)是否標(biāo)注了某個(gè)注解進(jìn)行過(guò)濾 assignable com.rookie.bigdata.XxxService 所有繼承或擴(kuò)展XxxService的類(lèi),該類(lèi)型采用目標(biāo)類(lèi)是否繼承或擴(kuò)展了某個(gè)特定類(lèi)進(jìn)行過(guò)濾 aspectj com.rookie.bigdata.*Service+ 所有類(lèi)名以Service結(jié)束的類(lèi)及繼承或擴(kuò)展它們的類(lèi),該類(lèi)采用AspectJ表達(dá)式進(jìn)行過(guò)濾 regex com.rookie.bigdata.annotation.* 所有com.rookie.bigdata.annotation類(lèi)包下的類(lèi),該類(lèi)型采用正則表達(dá)式根據(jù)目標(biāo)類(lèi)的類(lèi)名進(jìn)行過(guò)濾 custom com.rookie.bigdata.XxxTypeFilter 采用XxxTypeFile代碼方式實(shí)現(xiàn)過(guò)濾規(guī)則。該類(lèi)必須實(shí)現(xiàn)org.springframework.core.type.TypeFilter接口

<context:component-scan/>有一個(gè)容易忽視的use-default-filters屬性,默認(rèn)值為true;表示默認(rèn)會(huì)對(duì)標(biāo)注@Component、@Controller、@Service及Reposity的Bean進(jìn)行掃描,<context:component-scan/>先根據(jù)<exclude-filter>列出需要排除的黑名單,再通過(guò)<include-filter>列出需要包含的白名單。

自動(dòng)裝配Bean

Spring通過(guò)@Authwired注解實(shí)現(xiàn)Bean的依賴(lài)注入

如下:

@Repositorypublic class UserDao { public UserDao(){ System.out.println('實(shí)例化userDao'); }}

@Servicepublic class UserService { @Autowired private UserDao userDao;}

@Autowired默認(rèn)按照類(lèi)型(byType)匹配的方式在容器中查找匹配的Bean,當(dāng)有且僅有一個(gè)匹配的Bean時(shí),Spring將其注入@Autowired標(biāo)注的變量中

@Autowired還有一個(gè)required屬性,默認(rèn)情況下為true;表示必須找到匹配的Bean,否則會(huì)報(bào)NoSuchBeanDefinitionException異常。

@Autowired(required =true)private UserDao userDao;

@Qualifier注解

如果容器中有一個(gè)以上的匹配Bean時(shí),可以通過(guò)@Qualifier注解限定Bean的名稱(chēng)。如下:

@Repository@Qualifier(value = 'userDao')public class UserDao { public UserDao(){ System.out.println('實(shí)例化userDao'); }}

@Servicepublic class UserService { @Autowired(required =true) @Qualifier(value = 'userDao') private UserDao userDao;}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 双桥区| 明溪县| 岳普湖县| 舞阳县| 武义县| 大理市| 呈贡县| 城市| 舟曲县| 巧家县| 三台县| 靖西县| 田林县| 乐都县| 开江县| 漠河县| 迁安市| 武功县| 济源市| 江达县| 高密市| 江津市| 博白县| 同仁县| 博野县| 新建县| 云梦县| 盐津县| 孝感市| 崇义县| 枣庄市| 乐业县| 岳西县| 寿光市| 兴隆县| 凤山市| 赫章县| 永寿县| 黑山县| 吴桥县| 武义县|