<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                **1、AOP概念** 所說的面向切面編程其實就是在處理一系列業務邏輯的時候這一系列動作看成一個動作集合。比如連接數據庫來說: 加載驅動-----獲取class--------獲取連接對象-------訪問數據庫------查詢---------操作結果 對于上面的這一系列動作我們把其中的虛線看成是一個個的切面。然后我們在虛線的位置上加入一些邏輯。哪怕是日志,這也就成就了在不知不覺中將邏輯處理加入到了相應的位置上。而形成了所謂的面向切面編程! **下面通過@Before演示Aop織入到方法之前執行一些邏輯** AOP的應用: Xml配置 這里的xml配置引入了aop的xsd語法約束。另外加入的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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" > <context:annotation-config/> <!-- 配置容器資源掃描的包 --> <context:component-scan base-package="com.spring" /> <!-- 自動產生代理,內部實現是用AspectJ實現。可以使用aspectj的注解產生代理 --> <aop:aspectj-autoproxy /> </beans> ~~~ 加入Jar包: ![](https://box.kancloud.cn/2016-05-04_5729a8fe542b9.jpg) **1、編寫被注入的方法。必須得是spring容器管理的對象** ~~~ import org.springframework.stereotype.Component; import com.spring.dao.UserDao; @Component("userService") public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } //在下面方法前面加邏輯 public void HelloWorld(){ System.out.println("helloworld"); } } ~~~ **2、織入方法的配置:** ~~~ package com.spring.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; //聲明切面 @Aspect //加入Spring管理容器 @Component("LogInterceptor") public class LogInterceptor { //指定織入的方法。 @Before("execution(public void com.spring.service.UserServiceImpl.HelloWorld())") public void BeforeMethod(){ System.out.println("method start!"); } } 測試aop的織入: public class SpringTest { @Test public void test01() { BeanFactory applicationContext = new ClassPathXmlApplicationContext( "beans.xml"); UserService user = (UserService) applicationContext.getBean("userService"); user.HelloWorld(); } } ~~~ **注意:** 在編寫過程中的織入點語法上指定before織入哪個方法的前面。而括號中的這個被指定的織入點最好是實現一個接口。如果不實現接口的話就會報異常為: Cannot proxy target class because CGLIB2 is not available.Add CGLIB to the class path or specify proxy interfaces 這個異常的解決辦法為: 加入cglib.jar。因為被織入對象的方法單位如果沒有實現接口的話它就需要cglib.jar的支持。 **AOP基本概念:** JoinPoint:切入點、可以理解為上面案例的HelloWorld方法之前就為一個Joinpoint Pointcut:切入點的集合,可以通過織入點語法定義N個切入點加入邏輯處理。 Aspect:切面,指的是切面的類。也就是上面聲明Aspect的邏輯集合 Advise:指的是切面上的邏輯比如@Before、@After Target:被代理對象.上面的案例指的是UserServiceImpl Weave:織入
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看