Spring 框架第一天 applicationContext.xml配置和aop簡(jiǎn)單講解

一、Spring 框架配置文件applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


</beans>

二、Spring 框架 applicationContext.xml 文件的配置
1、依賴注入 set get 方法和無(wú)參構(gòu)造器必須具備

Paste_Image.png

2、依賴注入 只要求有無(wú)參構(gòu)造器

Paste_Image.png

3、參考注入

Paste_Image.png

4、復(fù)雜注入 數(shù)組、 list、 set、 map等復(fù)雜注入方式

<!-- 復(fù)雜注入 -->
    <bean id="tea" class="com.hw.entiyt.Teacher">
    <!-- 注入數(shù)組 -->
    <property name="info" >
    <array>
    <value>教授</value>
    <value>副教授</value>
    <value>講師</value>
    </array>
    </property>
    <!-- 注入List集合 -->
    <property name="list" >
    <list>
    <value>博士</value>
    <value>學(xué)士</value>
    <value>專科</value>
    </list>
    </property>
    <!-- 注入Set集合 -->
    <property name="set" >
    <set>
    <value>大一</value>
    <value>大二</value>
    <value>大三</value>
    </set>
    </property>
    <!-- 注入Map鍵值對(duì) -->
    <property name="map" >
    <map>
    <entry key="姓名" value="王大拿"></entry>
    <entry key="工資 " value="12000"></entry>
    <entry key="電話 " value="13810868978"></entry>
    </map>
    </property>
    </bean>

5、applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- spring依賴注入,第1種方式:set注入:屬性要有set get和無(wú)參的構(gòu)造方法 -->
    <bean id="stu1" class="com.hw.entiyt.Student">
        <property name="name" value="趙子龍"></property>
        <property name="score" value="100"></property>
    </bean>
    <!-- spring依賴注入,第2種方式:構(gòu)造方法 -->
    <bean id="stu2" class="com.hw.entiyt.Student">
        <!-- 注入時(shí)可以用index索引(從0開始),也可以用name -->
        <constructor-arg index="0" value="2"></constructor-arg>
        <constructor-arg index="1" value="李小龍"></constructor-arg>
        <constructor-arg name="score" value="99"></constructor-arg>
    </bean>
    <!-- spring依賴注入,第3種方式:參考注入(接口注入) -->
    <bean id="boy" class="com.hw.entiyt.Boy">
        <property name="name" value="張學(xué)良"></property>
    </bean>
    <bean id="girl" class="com.hw.entiyt.Girl">
        <property name="name" value="趙四小姐"></property>
        <property name="boy"  ref="boy"/><!-- 參考注入 -->
    </bean>
    <!-- 復(fù)雜注入 -->
    <bean id="tea" class="com.hw.entiyt.Teacher">
    <!-- 注入數(shù)組 -->
    <property name="info" >
    <array>
    <value>教授</value>
    <value>副教授</value>
    <value>講師</value>
    </array>
    </property>
    <!-- 注入List集合 -->
    <property name="list" >
    <list>
    <value>博士</value>
    <value>學(xué)士</value>
    <value>專科</value>
    </list>
    </property>
    <!-- 注入Set集合 -->
    <property name="set" >
    <set>
    <value>大一</value>
    <value>大二</value>
    <value>大三</value>
    </set>
    </property>
    <!-- 注入Map鍵值對(duì) -->
    <property name="map" >
    <map>
    <entry key="姓名" value="王大拿"></entry>
    <entry key="工資 " value="12000"></entry>
    <entry key="電話 " value="13810868978"></entry>
    </map>
    </property>
    </bean>
</beans>

三、aop 面向切面
1、applicationContext.xml 配置

Paste_Image.png

源碼

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
<bean id="dao" class="com.hw.dao.impl.StudentDaoImpl"></bean>
<bean id="service" class="com.hw.service.impl.StudentServiceImpl">
<property name="db"  ref="dao"></property>
</bean> -->
 <!-- 自動(dòng)掃描 -->
 <context:component-scan base-package="com.hw"/>
 <!-- 使用aop面向注解的代理方式 -->
 <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

2、aop 全注解

Paste_Image.png
Paste_Image.png
Paste_Image.png

@Resource 和 @Autowired 的區(qū)別

1、@Autowired與@Resource都可以用來(lái)裝配bean. 都可以寫在字段上,或?qū)懺趕etter方法上。 2、@Autowired默認(rèn)按類型裝配(這個(gè)注解是屬業(yè)spring的),默認(rèn)情況下必須要求依賴對(duì)象必須存在,如果要允許null 值,可以設(shè)置它的required屬性為false,如:@Autowired(required=false) ,如果我們想使用名稱裝配可以結(jié)合@Qualifier注解進(jìn)行使用,如下:

@Autowired() @Qualifier("baseDao")
private BaseDao baseDao;

3、@Resource(這個(gè)注解屬于J2EE的),默認(rèn)安照名稱進(jìn)行裝配,名稱可以通過(guò)name屬性進(jìn)行指定, 如果沒有指定name屬性,當(dāng)注解寫在字段上時(shí),默認(rèn)取字段名進(jìn)行按照名稱查找,如果注解寫在setter方法上默認(rèn)取屬性名進(jìn)行裝配。 當(dāng)找不到與名稱匹配的bean時(shí)才按照類型進(jìn)行裝配。但是需要注意的是,如果name屬性一旦指定,就只會(huì)按照名稱進(jìn)行裝配。

@Resource(name="baseDao")
private BaseDao baseDao;

我喜歡用 @Resource注解在字段上,且這個(gè)注解是屬于J2EE的,減少了與spring的耦合。最重要的這樣代碼看起就比較優(yōu)雅。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容