最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring AOP面向切面編程實現(xiàn)及配置詳解

 更新時間:2020年09月10日 09:41:20   作者:Jimmyhe  
這篇文章主要介紹了Spring AOP面向切面編程實現(xiàn)及配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

動態(tài)代理

特點

字節(jié)碼隨用隨創(chuàng)建,隨用隨加載

作用

不用修改源碼對方法增強

分類

基于接口的動態(tài)代理

基于子類的動態(tài)代理

創(chuàng)建

使用Proxy類中的newProxyInstance方法

要求

被代理類最少實現(xiàn)一個接口,沒有則不能使用

newProxyInstance方法參數(shù)

classLoader:類加載器

用于加載代理對象字節(jié)碼的,和被代理對象使用相同的類加載器

class[ ]:字節(jié)碼數(shù)組

用于讓代理對象和被代理對象有相同方法,固定寫法。

InvocationHandler:用于提供增強的代碼

是讓我們寫如何代理。一般都是寫一個該接口的實現(xiàn)類,通常情況下都是匿名內(nèi)部類,不是必須的

此接口的實現(xiàn)類都是誰用誰寫

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

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

AOPxml配置

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

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

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

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

在invoke方法里的,有前置通知,后置通知,異常通知,最終通知

引入Introduction

目標對象Target :即被代理的對象

織入Weaving:把增強應用到目標對象來創(chuàng)建新的代理對象的過程。Spring采用動態(tài)代理織入。

創(chuàng)建接口類,實現(xiàn)類

創(chuàng)建aop通知功能函數(shù)

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標簽的內(nèi)部使用對應的標簽來配置通知的類型
        現(xiàn)在讓pringLog方法在切入點方法執(zhí)行前執(zhí)行
        aop:before表示配置前置通知
          method:用于指定Logger類中哪個方法是前置通知
          point屬性:用于指定切入點表達式,該表達式指的是對業(yè)務層中哪些方法增強
          切入點表達式:
            關鍵字:execution(表達式)
            訪問修飾符 返回值 包名.類名.方法名(參數(shù)列表)
            全通配寫法:* *..*.*(..)
              訪問修飾符可以省略 *可以代表任何返回值 *.*.*可以表示包的關系 *..表示中間任意包 *.* 表示類名和方法
              (..)表示任意參數(shù)或者可以寫返回值類型 int, java.lang.String

            實際開發(fā)寫法:切到業(yè)務層實現(xiàn)類下的所有方法 * 業(yè)務層包.*.*(..)
    -->

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

    <!--配置AOP-->
    <aop:config>
      <!--配置切面-->
      <aop:aspect id="logAdvice" ref="logger">
        <!--配置通知類型,并且建立通知方法和切入點方法的關聯(lián)-->
        <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">
        <!--配置通知類型,并且建立通知方法和切入點方法的關聯(lián)-->
<!--        <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屬性用于指定表達式內(nèi)容,此標簽也可以放在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(){}
  /**
   * 用于打印日志:計劃在其切入點方法執(zhí)行前執(zhí)行(切入點方法就是業(yè)務層方法)
   */
  @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("最終");
  }

  // 環(huán)繞通知為我們提供了ProceedingJoinPoint,有一個方法proceed(),此方法就明確了調(diào)用切入點方法
  // 為我們提供了一種可以在代碼中手動控制增強方法合適執(zhí)行的方式
  public Object aroundPrintLog(ProceedingJoinPoint pjp) {
    Object returnValue = null;
    try {
      Object[] args = pjp.getArgs(); // 得到方法執(zhí)行所需參數(shù)

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

      returnValue = pjp.proceed(args); // 明確調(diào)用業(yè)務層的方法

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

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

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

xml:

配置spring創(chuàng)建容器要掃描的包

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

注意 如果用注解自帶的調(diào)用順序會出現(xiàn)問題,用環(huán)繞通知順序正常

事務控制

導包

<!-- 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,表示使用數(shù)據(jù)庫的默認隔離級別
		propagation:用于指定事物的傳播行為,默認是REQUIRED,表示一定會有事物,增刪改的選擇,查詢可以使用support
		read-only:用于指定事物是否只讀,查詢才設置為true
		timeout:用于指定事物的超市時間,默認值是-1,表示不超時,如果指定了數(shù)值,以秒為單位
		rollback-for:用于指定一個異常,當產(chǎn)生該異常時事物回滾,產(chǎn)生其他異常時,事物不回滾。沒有默認值,表示任何異常都回滾
		no-rollback-for:用于指定一個異常,當產(chǎn)生該異常,事務不會回滾,產(chǎn)生其他異常,事務回滾。沒有默認值,表示任何異常都回滾。
		
	</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創(chuàng)建容器時要掃描的包

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

4. 開啟spring對注解事物的支持

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

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

2.在實現(xiàn)類中

@Service(accountService)
@Transactional
public class 實現(xiàn)類 implements 接口類{
	@Autowired
	// 在持久層也要配置
	private IaccountDao accountDao
}

基于注解的配置類

1.創(chuàng)建一個配置總配置類

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

2.另一個java類,連接數(shù)據(jù)庫相關的類

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(){
		配置數(shù)據(jù)庫的用戶名密碼 創(chuàng)建數(shù)據(jù)源對象
	}
}

3. 新建一個properties,配置文件類

hibernate.username =
hibernate.password =

4. 創(chuàng)建和事物相關的配置類

public class TransactionConfig {
	//創(chuàng)建事務管理器對象
	@Bean(name="transactionManager")
	public PlatformTransactionManager createTransactionManager(DataSource dataSource){
		return new DataSourceTransactionManager(dataSource)
	}
}

5. main方法所在的類

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot多環(huán)境配置及日志記錄器詳解

    SpringBoot多環(huán)境配置及日志記錄器詳解

    這篇文章主要介紹了SpringBoot多環(huán)境配置及日志記錄器詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • Java 獲取當前系統(tǒng)時間的三種方法

    Java 獲取當前系統(tǒng)時間的三種方法

    這篇文章主要介紹了Java 獲取當前系統(tǒng)時間的三種方法,幫助大家利用Java處理時間,感興趣的朋友可以了解下
    2020-10-10
  • MyBatis中防止SQL注入講解

    MyBatis中防止SQL注入講解

    這篇文章主要介紹了MyBatis中防止SQL注入,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-12-12
  • SpringBoot RestTemplate GET POST請求的實例講解

    SpringBoot RestTemplate GET POST請求的實例講解

    這篇文章主要介紹了SpringBoot RestTemplate GET POST請求的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java多線程場景解析volatile和AtomicLong區(qū)別原理

    Java多線程場景解析volatile和AtomicLong區(qū)別原理

    這篇文章主要為大家介紹了Java中volatile和AtomicLong的區(qū)別原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Java中JDBC實現(xiàn)動態(tài)查詢的實例詳解

    Java中JDBC實現(xiàn)動態(tài)查詢的實例詳解

    從多個查詢條件中隨機選擇若干個組合成一個DQL語句進行查詢,這一過程叫做動態(tài)查詢。下面通過實例代碼給大家講解JDBC實現(xiàn)動態(tài)查詢的方法,需要的朋友參考下吧
    2017-07-07
  • Java全面細致講解final的使用

    Java全面細致講解final的使用

    關于final關鍵字,它也是我們一個經(jīng)常用的關鍵字,可以修飾在類上、或者修飾在變量、方法上,以此看來定義它的一些不可變性!像我們經(jīng)常使用的String類中,它便是final來修飾的類,并且它的字符數(shù)組也是被final所修飾的。但是一些final的一些細節(jié)你真的了解過嗎
    2022-05-05
  • Java?hutool?List集合對象拷貝示例代碼

    Java?hutool?List集合對象拷貝示例代碼

    這篇文章主要介紹了Java?hutool?List集合對象拷貝的相關資料,文章還分享了在實現(xiàn)過程中遇到的一些問題,并強調(diào)了閱讀源碼和正確配置CopyOptions的重要性,需要的朋友可以參考下
    2024-12-12
  • Java中的服務發(fā)現(xiàn)與負載均衡及Eureka與Ribbon的應用小結(jié)

    Java中的服務發(fā)現(xiàn)與負載均衡及Eureka與Ribbon的應用小結(jié)

    這篇文章主要介紹了Java中的服務發(fā)現(xiàn)與負載均衡:Eureka與Ribbon的應用,通過使用Eureka和Ribbon,我們可以在Java項目中實現(xiàn)高效的服務發(fā)現(xiàn)和負載均衡,需要的朋友可以參考下
    2024-08-08
  • Java類和對象習題及詳細答案解析

    Java類和對象習題及詳細答案解析

    這篇文章主要介紹了Java類和對象的相關知識,包括局部變量初始化、靜態(tài)方法、靜態(tài)導入、構(gòu)造方法、代碼塊執(zhí)行順序、toString方法重寫、類變量和靜態(tài)成員變量的訪問等,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-02-02

最新評論

始兴县| 西昌市| 丹寨县| 萨嘎县| 瑞昌市| 河北省| 朔州市| 喜德县| 炉霍县| 吴川市| 长垣县| 财经| 景东| 峨边| 华池县| 东丰县| 天门市| 双桥区| 潮安县| 探索| 合水县| 炎陵县| 夏邑县| 湾仔区| 玉溪市| 焦作市| 龙胜| 会泽县| 石城县| 乐平市| 富平县| 梁山县| 鄂托克前旗| 莲花县| 梁河县| 仪征市| 长寿区| 周至县| 景宁| 平原县| 久治县|