框架
3.png
spring
IOC AOP
配置文件
<?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-3.1.xsd">
<bean id="iUserDao" class="com.wxb.dao.UserDaoImp">
</bean>
<bean id="iUservice" class="com.wxb.service.UserService">
</bean>
</beans>
IOC/DI
依賴注入(Dependecy Injection)
控制反轉(zhuǎn)(Inversion of Control)
是同一個(gè)概念浸锨,具體的講:
當(dāng)某個(gè)角色需要另外一個(gè)角色協(xié)助的時(shí)候稠项,在傳統(tǒng)的程序設(shè)計(jì)過(guò)程中褂始,通常由調(diào)用者來(lái)創(chuàng)建被調(diào)用者的實(shí)例。但在spring中創(chuàng)建被調(diào)用者的工作不再由調(diào)用者來(lái)完成媒至,因此稱為控制反轉(zhuǎn)。創(chuàng)建被調(diào)用者的工作由spring來(lái)完成谷徙,然后注入調(diào)用者因此也稱為依賴注入拒啰。 spring以動(dòng)態(tài)靈活的方式來(lái)管理對(duì)象 , 注入的兩種方式完慧,設(shè)置注入和構(gòu)造注入谋旦。
實(shí)現(xiàn)方式
propety 方式實(shí)現(xiàn) 屬性set方法
private IUserDao dao;
public void setDao(IUserDao dao) {
this.dao = dao;
}
<property name="dao" ref="iUserDao"></property>
構(gòu)造方式實(shí)現(xiàn) 加constructor-arg 有參構(gòu)造方式
private IUserDao dao;
public UserService(IUserDao dao) {
this.dao = dao;
}
<constructor-arg>
<ref bean="iUserDao"/>
</constructor-arg>
p命名空間實(shí)現(xiàn):p:dao-ref="iUserDao" set 方法
<bean id="iUservice" class="com.wxb.service.UserService" p:dao-ref="iUserDao" >
自動(dòng)裝配 *autowire="byName" autowire="byType" 建議使用byName
- 局部
<bean id="iUservice" class="com.wxb.service.UserService" autowire="byName">
- 全局
2.png
注意 前三種優(yōu)先級(jí)高于自動(dòng)裝配
特殊字段 符號(hào)及集合 的處理方式

bean.png
- user
<bean id="user" class="com.wxb.bean.User">
<property name="name" value="張三">
</property>
<property name="password" value="123">
</property>
</bean>
- strs
<property name="strs">
<array>
<value>金星</value>
<value>火星</value>
<value>木星</value>
<value></value>
</array>
</property>
- list
<property name="strs">
<array>
<value>金星</value>
<value>火星</value>
<value>木星</value>
<value></value>
</array>
</property>
- set
<property name="set">
<set>
<value>中國(guó)</value>
<value>日本</value>
<value>俄羅斯</value>
</set>
</property>
- str
<property name="str">
<value><![CDATA[#$$%784582]]></value>
</property>
- pro
<property name="pro">
<props>
<prop key="北京">通州</prop>
<prop key="杭州">西湖</prop>
<prop key="上海">浦東</prop>
</props>
</property>
- map
<property name="map">
<map>
<entry>
<key>
<value>京</value></key>
<value>北京</value>
</entry>
<entry>
<key>
<value>浙</value></key>
<value>浙江</value>
</entry>
<entry>
<key>
<value>豫</value></key>
<value>河南</value>
</entry>
</map>
</property>
作用域
- prototype 原型 每次創(chuàng)建新的實(shí)例對(duì)象 多用于control層
<bean id="user" class="com.wxb.bean.User" scope="prototype">
- singleton 單例 只創(chuàng)建一次 多用于service dao
<bean id="user" class="com.wxb.bean.User" scope="singleton">
注解

注解.png
11.png

22.png
<!-- 自動(dòng)掃描-->
<context:component-scan base-package="com.wxb.dao,com.wxb.service">
</context:component-scan>

33.png
此時(shí)自動(dòng)裝配的時(shí)候使用的是bytype 所以注解內(nèi)可以不寫(xiě)
44.png
AOP 面向切面編程
核心思想:面向切面的編程思想
場(chǎng)景及作用:
- <1> 統(tǒng)計(jì)某個(gè)方法的運(yùn)行時(shí)間
- <2> 增強(qiáng)操作日志
- <3> 數(shù)據(jù)更新事物
AOP 編程思想理解
不在業(yè)務(wù)中寫(xiě)附加代碼,而是單獨(dú)寫(xiě)切面類屈尼;后期切面類和業(yè)務(wù)組裝配置
xml配置文件設(shè)置

111.png

112.png
<!-- aop配置 -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(public * com.wxb.service..*(..))" />
</aop:config>
<!-- 切面功能配置册着,和業(yè)務(wù)點(diǎn)組裝 -->
<aop:config>
<aop:aspect ref="demoUtil">
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
統(tǒng)計(jì)方法運(yùn)行時(shí)間
@Component("demoUtil")
public class DemoUtil {
private DemoUtil demoUtil;
public void setDemoUtil(DemoUtil demoUtil) {
this.demoUtil = demoUtil;
}
//環(huán)繞增強(qiáng)處理
public Object around(ProceedingJoinPoint pjp){
Object object=null;
//開(kāi)始時(shí)間
Date before=new Date();
//調(diào)用方法
try {
object = pjp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//結(jié)束時(shí)間
Date after=new Date();
//統(tǒng)計(jì)時(shí)間
System.out.println("運(yùn)行時(shí)間為:"+(after.getTime()-before.getTime()));
return object;
}
}
### 環(huán)繞增強(qiáng)處理 <aop:around> pjp.proceed();//調(diào)用原方法
``` xml
<aop:around method="around" pointcut-ref="pointcut"/>
前置增強(qiáng)處理
<aop:before method="before" pointcut-ref="pointcut"/>
最終增強(qiáng)處理 結(jié)論:無(wú)論異常與否,都能執(zhí)行
<aop:after method="after" pointcut-ref="pointcut"/>
后置增強(qiáng)處理 條件:不出現(xiàn)異常 <aop:after-returning>
<aop:after-returning method="afterReturn" pointcut-ref="pointcut" returning="obj"/>
異常增強(qiáng)處理 用途:報(bào)錯(cuò)信息日志脾歧;<aop:after-throwing>
<aop:after-throwing method="afterThrow" pointcut-ref="pointcut" throwing="exc"/>
Advisor增強(qiáng)處理
<aop:advisor advice-ref="logUtil" pointcut-ref="pointcut"/>
實(shí)現(xiàn)接口 MethodBeforeAdvice 實(shí)現(xiàn)接口方法