Spring設定檔方式實現Aop_spring設定檔Aoc

來源:互聯網
上載者:User

spring目錄檔案一覽:


需要的jar包一覽:


jar匯入以後,還需要build path,選中jar右鍵 build path -》add build path

首先建立Persion.java

package com.hpe.po;public class Person {// 需求,在打eating之前,列印方法開始// 列印eating只後,列印方法結束public void eat() {System.out.println("eat....");}public void drunk() {System.out.println("drunk....");}public void drunkBear() {System.out.println("drunkBear....");}public void play() {System.out.println("play....");}}


然後建立增強類:MyAdvice.java

package com.hpe.po;import org.aspectj.lang.ProceedingJoinPoint;public class MyAdvice {public void before() {System.out.println("方法開始");}public void end() {System.out.println("方法結束");}public void final1() {System.out.println("方法結束");}public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("方法bigin");joinPoint.proceed();System.out.println("方法end");}}


配置log4j.xml

# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

最後設定檔:

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        "><!-- xml方式實現aop --><!-- 無參構造方法bean管理 --><bean id="person" class="com.hpe.po.Person"></bean><bean id="myAdvice" class="com.hpe.po.MyAdvice"></bean><!-- aop配置 --><aop:config><!-- 配置切入點 id:標識 expression:運算式 execution(方法修飾符? 方法傳回值 方法名(參數))傳回值方法名參數不能少 *代表任意 切入點是在執行expression運算式攔截到的點,或者說執行該方法的時刻,因為在執行該點前後我們要切入加強方法--><aop:pointcut expression="execution(* com.hpe.po.Person.eat(..))"id="pointcut1" /><aop:pointcut expression="execution(* com.hpe.po.Person.drunk*(..))"id="pointcut2" /><aop:pointcut expression="execution(* com.hpe.po.Person.play(..))"id="pointcut3" /><!-- 配置切面:將增強(前置後置。。)應用到切入點的過程 --><!-- ref="myAdvice"是我們用bean管理的書寫增強方法的管理類檔案 --><aop:aspect ref="myAdvice"><!-- before:之後執行 --><aop:before method="before" pointcut-ref="pointcut1" /><!-- 執行次序 --><aop:after method="end" pointcut-ref="pointcut1" /> <aop:after-returning method="final1"pointcut-ref="pointcut2" /><aop:around method="around" pointcut-ref="pointcut3" /> </aop:aspect></aop:config></beans>

書寫test類:

package com.hpe.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.hpe.po.Person;public class AOPTest {@Testpublic void test1(){ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//Persion p=(Persion)context.getBean("persion1");Person person = context.getBean("person", Person.class);person.eat();//person.drunk();}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.