package com.spring.jdkTest;
public interface Iuser {
void printOne();
void printTwo();
}
package com.spring.jdkTest;
public class UserImpl implements Iuser {
@Override
public void printOne() {
System.out.println("One");
}
@Override
public void printTwo() {
System.out.println("Two");
}
}
package com.spring.advice;
import java.lang.invoke.MethodHandleInfo;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
public class Advice implements org.aopalliance.intercept.MethodInterceptor{
@Override
public Object invoke(MethodInvocation pass) throws Throwable {
System.out.println("前");
Object proceed =pass.proceed();
System.out.println("后");
return proceed;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--设置目标类对象 -->
<bean id="targetObject" class="com.spring.jdkTest.UserImpl"></bean>
<!--增强通知类的对象 -->
<bean id="myAdvice" class="com.spring.advice.Advice"></bean>
<aop:config>
<!--aop:pointcut 看起来像连接点 -->
<!--execution(* com.xyz.myapp.service.*.*(..)) 切入点表达式
第一个* 号代表方法的返回值任意
第二个* service包下的任意类
第三个* 任意类下的任意方法
(..)。。 方法里面的参数任意
-->
<!-- <aop:pointcut expression="execution(* com.spring.jdkTest.*.*(..))" id="myPointCut"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/> -->
<!-- 使任意方法变为其中的一个方法,制定方法增强 -->
<aop:pointcut expression="execution(* com.spring.jdkTest.*.printOne(..))" id="myPointCut"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/>
</aop:config>
</beans>
package com.spring.jdkTest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest{
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Iuser user = context.getBean("targetObject",Iuser.class);
user.printOne();
user.printTwo();
}
}