Spring 框架之IOC、AOP

Spring 框架之IOC、AOP

spring概念

spring是項目中對象的容器.用來管理項目中的所有對象.

  1. 創建對象,並維護對象間的關係AOP
  2. 整合代理技術,實現AOP思想
  3. spring可以整合幾乎任何第三方框架,作為項目中的粘合劑.

IOC&DI概念

  • IOC:反轉控制,創建對象的方式被反轉,從我們自己創建變為程序創建.
  • DI:依賴注入,是技術,可以將依賴屬性注入到對象.構造注入|set方法注入.IOC的實現需要依賴DI技術.

BeanFactory&ApplicationContext

  • BeanFactory接口
  • BeanFactory是spring框架初代接口.
  • BeanFactory是ApplicationContext父接口.
  • BeanFactory是獲得對象時才創建對象.
  • ApplicationContext接口
  • ApplicationContext是後期發展出的接口.
  • ApplicationContext的功能要更加強大.
  • ApplicationContext是啟動時默認立刻創建容器中所有對象,獲得時直接返回

AOP思想

  • 縱向重複,橫向抽取.
  • spring封裝了代理技術,通過代理技術來體現aop思想.我們使用代理體現aop時,不需要我們自己書寫代理代碼.spring幫我們完成.

spring框架搭建

導包

  • 4個核心包,core、bean、context、exception
  • 2個日誌包,apache.commons.loggin、apache.log4j

準備對象

創建一個類

創建spring配置文件

在src目錄下創建applicationContext.xml

導入4個約束(schema)beans、context、tx、aop

配置spring容器隨項目啟動

原則:一個web項目啟動只創建一個spring容器.

操作:

方案一:將spring容器的創建放入靜態代碼塊中.該訪問屬於自己手動創建容器,容器與web項目沒有關聯,不推薦.

方案二:spring利用監聽器(ServletContext對象創建與銷燬監聽器)來創建容器.

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

原理:ContextLoaderListener繼承了ServletContextListener,ServletContextListener監聽ServletContext的創建與銷燬。

代碼測試

SE環境測試

//1.創建spring容器

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

//2.獲取User對象

User user = ac.getBean("user");

獲得spring容器及容器中對象

Web環境下

//1.獲得Application域對象

ServletContext sc = ServletActionContext.getServletContext();

//2.調用工具方法從域中取出對象

ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);

//3.獲得CustomerService

us = (UserService) ac.getBean("userService");

spring配置詳解(xml)

基礎配置

bean元素:將對象交給spring容器管理

  • name屬性:對象的名字,spring根據該名字獲得。
  • id屬性:對象的名字,spring根據該名字獲得。
  • name屬性與id屬性區別:
  • 1.id出現時間較早.
  • 2.id屬性必須符合id規範:唯一不能使用特殊字符.
  • 3.name屬性:可以重複(不推薦重複),也可以使用特殊字符.
  • class屬性:對象的完整類名。
  • scope屬性:決定對象的作用域,生命週期。
  • singleton(默認值):單例,容器啟動時創建單例對象,容器銷燬時才銷燬單例對象.
  • prototype:原型多例,啟動時不會創建,每次獲得時都會創建新的對象,對象不會放入容器的管理.
  • request:web環境下的屬性,對象的生命週期為一次請求.
  • session:web環境下的屬性,對象的生命週期為一次會話.
  • init-method|desotory-method屬性:指定對象的生命週期。
  • init-method:指定對象的初始化方法,會在構造方法調用之後調用.
  • destory-method:指定對象的銷燬方法,會在容器銷燬對象之前調用.
  • xml配置

sping創建對象方法

靜態工廠

//靜態工廠

public class UserFactory{

public static User getUser(){

return new User();

}

}

xml配置文件

動態工廠

//動態工廠

public class UserFactory{

public User getUser2(){

return new User();

}

}

xml配置文件

構造方法(推薦)

spring中的DI(依賴注入)

構造方法注入

constructor-arg:指定構造參數

  • 定位屬性:
  • name屬性:參數名
  • index屬性:指定參數在參數列表中的索引
  • type屬性:指定參數類型
  • 注入屬性:
  • value屬性:參數值
  • ref屬性:注入引用類型

set方法注入

  • property標籤
  • p名稱空間

導入p名稱空間

xmlns:p="http://www.springframework.org/schema/p"

p:屬性名= | p:屬性名-ref=

SPEL表達式

SPEL:spring expression language spring 表達式語言,只能引用屬性,不能引用對象.

在配置文件中使用#{spel}來引用其他Bean的屬性值.

複雜類型注入

Array

1.只有一個值可以直接使用value|ref

2.如果多個值,元素

pro

List

1.只有一個值可以直接使用value|ref

2.如果多個值,元素

|-

|-

c

Map

|-填寫鍵值對

Properties

|-填寫鍵值對

spring配置詳解(註解)

準備工作

導包

|4+2

|spring-aop

導入約束

spring-context-4.2.xsd

開啟註解配置

註解

註冊對象

@component(“user”)

@controller(“user”)

@service(“user”)

@Repository(“user”)

指定對象域作用域

@Scope(“singleton”)

指定對象生命週期

@PostConstruct 初始化

@PreDestory 銷燬

值注入

@value

1.成員變量,直接為成員變量賦值

2.set方法,調用set方法賦值

注入引用類型

1.@AutoWired根據類型自動裝配

2.@AutoWired和@Qulifier配合,@Qualifier(“car”)

3.@Resource直接指定注入的對象名,@Resource(name=”car”)

spring整合JDBC(JDBCTemplate)

spring框架提供一個可以完成jdbc操作的對象,該對象可以放置到spring容器中.

spring準備的操作數據庫的類,與QueryRunner類的設計與使用幾乎完全相同.

導包

  • 4+2
  • spring-jdbc、spring-tx
  • 數據庫驅動、c3p0連接池

JDBCTemplate工具類

  • jt.update(String sql,Object…params)增刪改
  • public void save(User u){
  • String sql ="insert into t_user value(null,?) ";
  • getJdbcTemplate().update(sql,user.getName);
  • }
  • jt.queryForObject(sql,class,params),查詢單個值,並指定返回值類型
  • //查詢總數
  • public Integer getTotalCount(){
  • String sql = "select Count(*) from t_user ";
  • getJdbcTemplate().queryForObject(sql,Integer.class);
  • }
  • jt.queryForObject(sql,rowMapper,params),執行查詢,並手動封裝結果到對象中.返回單個對象
  • //根據id查對象
  • public User getById(Integer id){
  • String sql = "select * from t_user where id=?";
  • return getJdbcTemplate().QueryForObject(sql,new RowMapper(){
  • public User mapRow(ResultSet rs,int index ) throws SQLException{
  • User user = new User();
  • user.setId(rs.getInt("id"));
  • user.setName(rs.getString("name"));
  • return user;
  • }
  • },id);
  • }
  • jt.query(sql,rowMapper,params),執行查詢,手動封裝結果,返回List
  • //查詢所有的User
  • public List getAll(){
  • String sql = "select * from t_user";
  • return getJdbcTemplate().QueryForObject(sql,new RowMapper(){
  • public User mapRow(ResultSet rs,int index) throws SQLException{
  • User user = new User();
  • user.setId(rs.getInt("id"));
  • user.setName(rs.getString("name"));
  • return user;
  • }
  • });
  • }

JdBCDaoSupport

繼承JdbcDaoSupport,減少一層依賴關係.源碼中已經幫我們接收JdbcTemplate

好處:可以減少一級依賴關係.直接將連接池對象注入給Dao即可。

使用DB.properties管理連接信息

  1. 在src下創建properties文件,文件配置數據庫信息
  2. spring配置文件中讀取:
  3. 在配置中引用,${key}

spring中aop開發

aop&代理

spring中封裝的代理技術

  • 動態代理
  • 侷限性:生成代理類時必須基於接口.生成的代理對象實際上就是接口的實現類.
  • spring中的aop是希望能夠對所有對象生成代理的.基於該動態代理原則會導致
  • 項目中很多類無法生成代理.
  • CGLib代理
  • spring為了能夠對項目中所有類生成代理.所以引入了cglib這個第三方代理技術.該代理技術的特點是.對目標對象生成代理時,代理對象是被代理對象的子類.
  • hibernate中應用了CGLib代理,懶加載技術中返回的就是cglib代理對象.

aop中的名稱解釋

aop聯盟制定規範

  • 連接點 join point:目標對象中所有可以增強的方法.
  • 切點 point cut:已經或即將增加的方法.
  • 通知 advice:我們需要對目標方法增強的代碼.
  • 目標對象 target:我們需要增強的對象
  • 代理對象 proxy:將通知應用到目標對象生成的對象
  • 織入 weaving:將通知織入到目標對象的過程.
  • 切面 aspect|advistor:切點+通知

springAOP開發步驟(註解)

  • 導包
  • 目標對象
  • 通知對象
  • 配置註解

註解詳解

xml中配置

  1. 註冊目標對象
  1. 註冊通知對象
  1. 開啟使用註解配置aop

代碼中配置註解

  • @Aspect 表示該類是通知類
  • @Aspect
  • pbublic class MyAdvice{}
  • @Before(“execution(表達式)”) 前置通知
  • @Around(“execution(表達式)”) 環繞通知
  • @AfterRunner(“execution(表達式)”) 後置通知
  • @After(“execution(表達式)”) 後置通知,不管是否拋異常,都執行
  • @AfterThrowing(“execution(表達式)”) 異常攔截通知
  • //註冊切點
  • @Pointcut(execution(* cn.it.service.impl.*ServiceImpl.*(..)))
  • public void myPc(){}
  • //前置通知
  • @Before("MyAdvice.myPc()")
  • public void before(){}

springAOP開發步驟(xml)

  • 導包
  • 目標對象
  • 通知對象
  • 配置xml

xml配置詳解

Spring中的aop事務管理

事務屬性

  • 傳播行為(PROPAGATION)默認選擇REQUIRED
  • 是否只讀(only-read)true|false
  • 隔離級別

springAOP事務管理(XML) ###

  • 導包
  • 創建Dao層對象
  • 創建Service層對象
  • 創建Spring配置文件

spring文件配置詳解

  • 配置事務

引入tx約束

springAOP事務管理(註解) ###

  • 導包
  • 創建Dao層對象
  • 創建Service層對象
  • 創建Spring配置文件

spring文件配置詳解

  • 配置註解
  • //使用註解管理事務
  • @Transactional(paragation=Paragation.REQUIRED,readOnly=false,isolaction=Isolaction.REPEATABLE_READ)
  • public class UserServiceImpl implements UserService{
  • @Transactional(paragation=Paragation.REQUIRED,readOnly=true,isolaction=Isolaction.REPEATABLE_READ)
  • public void transaction()
  • }

spring整合hibernate、struts2

導包

  • hibernate lib下required
  • struts2 apps下struts2-blank(刪除與hibernate相同的包)
  • spring
  1. core、context、bean、exception、log4j、logging
  2. spring-jdbc、spring-tx、spring-orm
  3. 數據庫驅動、c3p0連接池
  4. spring-aop、spring-aspect
  5. aspectj織入、aop聯盟
  6. spring-web
  7. spring-test、junit4
  • web jstl

整合spring到web項目

配置監聽器,讓spring隨項目的啟動而創建容器

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

準備spring配置文件

在src目錄下創建applicationContext.xml,導入4個約束,bean、context、tx、aop

整合struts2到web項目

創建Action

  • 創建Action的三種方式
  1. pojo(普通java對象,不需要實現任何接口|任何類)
  2. 實現Action
  3. 繼承ActionSupport
  • 獲取表單數據的三種方式
  1. 屬性驅動
  2. 對象驅動
  3. 模型驅動

創建配置文件

/login.jsp

配置入口過濾器

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

spring整合struts2

配置整合spring常量

struts.objectFactory

spring

方案1:Action對象仍然由struts2創建,spring負責組裝依賴.

struts.objectFactory.spring.autoWire

name

在action中準備依賴屬性

方案2:Action對象創建與依賴注入全都由spring完成

將Action配置到spring容器中

在Action配置中的class屬性,填寫對象的BeanName

/login.jsp

搭建Hibernate框架

準備實體與映射文件

準備實體

映射文件 User.hbm.xml

準備主配置文件

在src目錄下創建hibernate.cfg.xml

整合spring與hibernate

整合方式1:使用原生Hibernate配置文件

整合方式2:將hibernate中的配置轉移到spring中

將c3p0連接池配置到spring

1.準備DB.properties

2.讀取db.properties

3.配置連接池對象,並注入連接配置

4.將連接池對象注入到sessionFactory中

將Dao&Hibernate模板配置到spring

書寫Dao類

配置hibernate模板到spring

配置dao到spring

整合AOP事務

1.完成Service

2.配置核心事務管理器

XML|註解事務配置

xml配置

註解

service中使用註解

@Transaction(progation=Progation.REQUIRED,readOnly=true,isolaction=Isolaction.REPEATABLE_READ)

publlic class UserServiceImpl implments UserService{

}

解決no-session問題

配置擴大session作用範圍的過濾器

OpenSessionInViewFilter

org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

OpenSessionInViewFilter

/*


分享到:


相關文章: