1. 程式人生 > 程式設計 >Spring AOP面向切面程式設計實現及配置詳解

Spring AOP面向切面程式設計實現及配置詳解

動態代理

特點

位元組碼隨用隨建立,隨用隨載入

作用

不用修改原始碼對方法增強

分類

基於介面的動態代理

基於子類的動態代理

建立

使用Proxy類中的newProxyInstance方法

要求

被代理類最少實現一個介面,沒有則不能使用

newProxyInstance方法引數

classLoader:類載入器

用於載入代理物件位元組碼的,和被代理物件使用相同的類載入器

class[ ]:位元組碼陣列

用於讓代理物件和被代理物件有相同方法,固定寫法。

InvocationHandler:用於提供增強的程式碼

是讓我們寫如何代理。一般都是寫一個該介面的實現類,通常情況下都是匿名內部類,不是必須的

此介面的實現類都是誰用誰寫

IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),producer.getClass().getInterfaces(),
	new InvocationHandler(){
	 作用:執行被代理物件的任何介面方法都會經過該方法
	 * proxy 代理物件的引用
	 * method 當前執行的方法
	 * args 執行當前方法所需的引數
	 * return 和被代理物件有相同的返回值
		@override
		public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
			// 提供增強的程式碼
			Object returnValue = null
			1. 獲取方法執行的引數
			Float money = (Float)args[0]
			2. 判斷當前方法是否為指定方法
			if("saleProduct".equals(method.getName())){
				returnValue = method.invoke(producer,money*0.8)
			}
			return returnValue;
		}
	}
)
//代理方法呼叫的是上面invoke中的方法
proxyProducer.saleProduct(100000)

注意 如果代理的類沒有介面,則代理不可用。

AOPxml配置

連線點Joinpoint:指那些被攔截的點,在spring中,這些點指的是方法,因為spring只支援方法型別的連線點。

切入點Pointcut:所謂切入點指的是要對哪些Joinpoint進行攔截的定義。方法會被增強。

所有的切入點都是連線點,但不是所有的連線點都是切入點。

通知Advice:指攔截到Joinpoint之後所要做的事情

在invoke方法裡的,有前置通知,後置通知,異常通知,最終通知

引入Introduction

目標物件Target :即被代理的物件

織入Weaving:把增強應用到目標物件來建立新的代理物件的過程。Spring採用動態代理織入。

建立介面類,實現類

建立aop通知功能函式

xml配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置spring的IOC,把service物件配置進來-->
    <bean id="accountService" class="hjj.web.service.impl.AccountServiceImpl"></bean>

    <!--spring中基於xml的aop配置步驟
      1. 把通知bean也交給spring來管理
      2. 使用aop:config標籤表明aop的配置
      3. 使用aop:aspect標籤表明配置切面
        id:給切面提供一個唯一表示
        ref:指定通知類bean的id
      4. 在aop:aspect標籤的內部使用對應的標籤來配置通知的型別
        現在讓pringLog方法在切入點方法執行前執行
        aop:before表示配置前置通知
          method:用於指定Logger類中哪個方法是前置通知
          point屬性:用於指定切入點表示式,該表示式指的是對業務層中哪些方法增強
          切入點表示式:
            關鍵字:execution(表示式)
            訪問修飾符 返回值 包名.類名.方法名(引數列表)
            全通配寫法:* *..*.*(..)
              訪問修飾符可以省略 *可以代表任何返回值 *.*.*可以表示包的關係 *..表示中間任意包 *.* 表示類名和方法
              (..)表示任意引數或者可以寫返回值型別 int,java.lang.String

            實際開發寫法:切到業務層實現類下的所有方法 * 業務層包.*.*(..)
    -->

    <!--配置logger類-->
    <bean id="logger" class="hjj.web.utils.Logger"></bean>

    <!--配置AOP-->
    <aop:config>
      <!--配置切面-->
      <aop:aspect id="logAdvice" ref="logger">
        <!--配置通知型別,並且建立通知方法和切入點方法的關聯-->
        <aop:before method="printLog" pointcut="execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
      </aop:aspect>
    </aop:config>
    
    // 通知型別
          <aop:aspect id="logAdvice" ref="logger">
        <!--配置通知型別,並且建立通知方法和切入點方法的關聯-->
<!--        <aop:before method="printLog" pointcut="execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:before>-->
        <aop:before method="beforePrintLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
        <aop:after-returning method="afterPrintLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:after-returning>
        <aop:after-throwing method="afterThrowingPringLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:after-throwing>
        <aop:after method="finalPrintLog" pointcut="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"></aop:after>
      </aop:aspect>
  
</beans>
<!-- 配置切入點表示式,ID屬性用於指定表示式的唯一標識,expression屬性用於指定表示式內容,此標籤也可以放在aspect外面-->
      <aop:pointcut id="pt1" expression="execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())"/>
      
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>

AOPxml註解

aop註解配置

/**
 * 記錄日誌的工具類,提供了公共的程式碼
 */
@Component("logger")
@Aspect // 表示當前類是一個切面
public class Logger {

		@Pointcut("execution()")
		private void pt1(){}
  /**
   * 用於列印日誌:計劃在其切入點方法執行前執行(切入點方法就是業務層方法)
   */
  @Before(pt1())
  public void beforePrintLog() {
    System.out.println("前置");
  }

  public void afterPrintLog() {
    System.out.println("後置");
  }

  public void afterThrowingPringLog() {
    System.out.println("異常");
  }

  public void finalPrintLog() {
    System.out.println("最終");
  }

  // 環繞通知為我們提供了ProceedingJoinPoint,有一個方法proceed(),此方法就明確了呼叫切入點方法
  // 為我們提供了一種可以在程式碼中手動控制增強方法合適執行的方式
  public Object aroundPrintLog(ProceedingJoinPoint pjp) {
    Object returnValue = null;
    try {
      Object[] args = pjp.getArgs(); // 得到方法執行所需引數

      System.out.println("前置");

      returnValue = pjp.proceed(args); // 明確呼叫業務層的方法

      System.out.println("後置");

    } catch (Throwable throwable) {
//      throwable.printStackTrace();
      System.out.println("異常");
    } finally {
      System.out.println("最終");
    }
    return returnValue;

//    System.out.println("環繞通知");
  }
}

xml:

配置spring建立容器要掃描的包

<context:component-scan base-package="包路徑"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

注意 如果用註解自帶的呼叫順序會出現問題,用環繞通知順序正常

事務控制

導包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.2.4.RELEASE</version>
</dependency>

事務管理器:org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager

在bean.xml中配置

1. 配置事物管理器

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="transactionManager" class="org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager">
	<property name="dataSource" ref="dataSource">
<bean>

2.配置事物的通知

<tx:advice id="txAdvice" transaction-manager="transactionManager">

5.配置事物的屬性

	<tx:attributes>
		<tx:method name="*" propagation="required" read-only='false'/>
		<tx:method name="find*" propagation="support" read-only='true'/>
		
		isolation:指定事物的隔離級別,預設值是default,表示使用資料庫的預設隔離級別
		propagation:用於指定事物的傳播行為,預設是REQUIRED,表示一定會有事物,增刪改的選擇,查詢可以使用support
		read-only:用於指定事物是否只讀,查詢才設定為true
		timeout:用於指定事物的超市時間,預設值是-1,表示不超時,如果指定了數值,以秒為單位
		rollback-for:用於指定一個異常,當產生該異常時事物回滾,產生其他異常時,事物不回滾。沒有預設值,表示任何異常都回滾
		no-rollback-for:用於指定一個異常,當產生該異常,事務不會回滾,產生其他異常,事務回滾。沒有預設值,表示任何異常都回滾。
		
	</tx:attributes>
</tx:advice>

3.配置aop切入點表示式

<aop:config>
<aop:pointcut id="pt1" expression="execute(* 包.包.*.*(..))">

4. 建立切入點表示式喝事物通知的對應關係

<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">
</aop>

<beans>

基於註解的事務控制

1. 配置事物管理器

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd"
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd">

3. 配置spring建立容器時要掃描的包

<context:component-scan base-package="包的地址">

4. 開啟spring對註解事物的支援

<tx:annotation-driven transaction-manager="transactionManager>"

6. 在需要事物支援的地方使用註解@Transactional

2.在實現類中

@Service(accountService)
@Transactional
public class 實現類 implements 介面類{
	@Autowired
	// 在持久層也要配置
	private IaccountDao accountDao
}

基於註解的配置類

1.建立一個配置總配置類

@Configuration
// 用於配置需要掃描的包
@ComponentScan("hjj.web")
@Import({HibernateConfig.class,TransactionConfig.class})
@PropertySource("hibernateConfig.properties")
@EnableTransactionManagement //開啟註解的支援
public class SpringConfiguration{
	
}

2.另一個java類,連線資料庫相關的類

publci class HibernateConfig{

	@Value("${hibernate.username}")
	private String username;
	@Value("${hibernate.password}")
	private String password

	// 注入進容器
	@Bean(name="HibernateTemplate")
	public Hibernate crateHibernateTemplate(DataSource datasource){
		return new HibernateTemplate(dataSource)
	}
	
	@Bean(name="dataSource")
	public DataSource crateDataSource(){
		配置資料庫的使用者名稱密碼 建立資料來源物件
	}
}

3. 新建一個properties,配置檔案類

hibernate.username =
hibernate.password =

4. 建立和事物相關的配置類

public class TransactionConfig {
	//建立事務管理器物件
	@Bean(name="transactionManager")
	public PlatformTransactionManager createTransactionManager(DataSource dataSource){
		return new DataSourceTransactionManager(dataSource)
	}
}

5. main方法所在的類

@ContextConfiguration(classes=SpringConfiguration.class)
public class test{
	psvm{
		業務邏輯
	}
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。