spring2.5AOP編程之xml

來源:互聯網
上載者:User

1:匯入jar包同註解AOP,有些應該沒用到。沒仔細去測試

2:1,定義自己的業務類。
介面:

public interface Test {</p><p> public void addMetgod(); </p><p>}</p><p>

實現:
public class Testimpl implements Test {</p><p> public void addMetgod() {<br /> System.out.println("---addMethod方法的實現-----");</p><p> }</p><p>}</p><p>

2,定義切面,也就是驗證方法:
介面:

public interface MySecurityManager {</p><p> public void security(); </p><p>}<br />
實現:
public class MySecurityManagerImpl implements MySecurityManager {</p><p> public void security() {<br /> System.out.println("-----調用security方法-------");<br /> }<br />}</p><p>

3配置:applicationContext-aop.xml
<?xml version="1.0" encoding="utf-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xmlns:aop="http://www.springframework.org/schema/aop"<br /> xmlns:tx="http://www.springframework.org/schema/tx"<br /> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"<br /> default-autowire="byName" default-lazy-init="true"></p><p><aop:aspectj-autoproxy/><br /> <bean id = "mySecurityManager" class = "test.a.MySecurityManagerImpl" ></bean ><br /> <aop:config><br /> <aop:aspect ref="mySecurityManager"><br /> <aop:before pointcut-ref="needse" method="security"/></p><p><!--註解:運算式的含義同註解配置一樣</p><p> 前置,後置等通知的配置方式都一樣</p><p> execution(* test.a.Test.add*(..)運算式的寫法</p><p> 只攔截到傳回型別為string的:execution(java.lang.String test.a.Test.add*(..)</p><p> 只攔截第一個參數為String的:execution(* test.a.Test.add*(java.lang.String,..)</p><p> 攔截傳回值不為void的:execution(!void test.a.Test.add*(..) --><br /> <aop:pointcut id="needse" expression_r="execution(* test.a.Test.add*(..))"/><br /> </aop:aspect><br /> </aop:config><br /> <bean id = "test" class = "test.a.Testimpl" ></bean ><br /> </beans></p><p>

4,測試:
package test.a;</p><p>import org.springframework.context.ApplicationContext;<br />import org.springframework.context.support.ClassPathXmlApplicationContext;</p><p>public class TestAop {</p><p> private static Test test;<br /> static {<br /> ApplicationContext cx = new ClassPathXmlApplicationContext(<br /> "applicationContext-aop.xml");<br /> test = (Test) cx.getBean("test");<br /> }</p><p> public static void main(String[] args) {<br /> test.addMetgod();<br /> }<br /> }</p><p>

 結果:
-----調用security方法-------
---addMethod方法的實現-----

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.