<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                上篇講述了[Spring的AOP](http://blog.csdn.net/lovesummerforever/article/details/22664647)[原理](http://blog.csdn.net/lovesummerforever/article/details/22664647),本篇將上篇中使用的動態代理解決獨立服務的問題用SpirngAOP來實現。 ### 采用配置文件的方式。 1、導入相應的Spring jar包。 2、[在SpringIOC中的步驟123中已經給出](http://blog.csdn.net/lovesummerforever/article/details/22646793)。 3、將橫切性關注的問題模塊化,建立安全處理類。在SecurityHandler類中寫我們的獨立方法,也就是定義Advice(具體實現),代碼如下。 ~~~ public class SecurityHandler { private void checkSecurity() { System.out.println("-------checkSecurity-------"); } } ~~~ 4、在配置文件中進行相關配置。applicationContext.xml代碼如下所示。 ~~~ <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/> <bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/> <aop:config> <aop:aspect id="secutityAspect" ref="securityHandler"> <!-- 以add開頭的方法 <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> --> <!-- com.bjpowernode.spring.*包下的所有方法. <aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.*(..))"/> --> <aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.add*(..)) || execution(* com.bjpowernode.spring.*.del*(..))"/> <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/> </aop:aspect> </aop:config> </beans> ~~~ 需要說明幾點 指定SecutityHander為aspect:????<aop:aspectid="secutityAspect" ref="securityHandler"> Pointcut(切入點)以及事務范圍,在這里用于所有的add和del方法上:<aop:pointcutid="addAddMethod" expression="execution(*com.bjpowernode.spring.*.add*(..)) || execution(*com.bjpowernode.spring.*.del*(..))"/> 設置通知類型并指向哪個切入點: <aop:beforemethod="checkSecurity" pointcut-ref="addAddMethod"/> 將目標類UsemrManagerImpl和Aspect類SecurityHandler配置到Spring的IOC中:<beanid="userManager"class="com.bjpowernode.spring.UserManagerImpl"/> <bean id="securityHandler"class="com.bjpowernode.spring.SecurityHandler"/> 5、客戶端調用代碼如下所示。 ~~~ import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); userManager.delUser(1); } } ~~~ 上述基本上完成了一個SpringAOP的實例。 ### 如果我們在advice中,也就是在我們的安全性方法中想要獲取客戶端的數據該如何獲取呢? 我們可以在這個方法中添加參數,也就是我們JoinPoint,通過傳遞這個對象,我們可以去得到參數值,SecurityHandler類的代碼如下所示。 ~~~ public class SecurityHandler { private void checkSecurity(JoinPoint joinPoint) { //取得參數. for(int i=0;i<joinPoint.getArgs().length;i++) { System.out.println(joinPoint.getArgs()[i]); } System.out.println(joinPoint.getSignature().getName()); System.out.println("-------checkSecurity-------"); } } ~~~ ### 當然我們也可以采用注解的方式來實現。 1、采用注解的方式首先要加入一些jar包,*Spring_home/spring-framework-2.0\lib\aspectj\aspectjrt.jar?aspectjweaver.jar 2、SecurityHandler類的代碼如下所示。和上面的通過配置文件方式進行對比,很快就會懂得不同的標簽不同的含義。 ~~~ import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class SecurityHandler { /** * 定義Pointcut,Pointcut的名稱為addAddMethod(),此方法沒有返回值和參數 * 該方法就是一個標識,不進行調用 */ @Pointcut("execution(* add*(..))") private void addAddMethod(){}; /** * 定義Advice,表示我們的Advice應用到哪些Pointcut訂閱的Joinpoint上 */ @Before("addAddMethod()") //@After("addAddMethod()") private void checkSecurity() { System.out.println("-------checkSecurity-------"); } } ~~~ 3、applicationContext.xml文件中配置aspect類和目標類UserManagerImpl,同時配置實用Annotation方式,代碼如下所示。 ~~~ <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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 啟用AspectJ對Annotation的支持 --> <aop:aspectj-autoproxy/> <bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/> <bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/> </beans> ~~~ 4、客戶端調用和配置XML方式一樣。 是不是關于SpirngAOP,漸漸的懂些了。
                  <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>

                              哎呀哎呀视频在线观看