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 configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.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 {
@Test
public 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();
}
}