第一种方式:基于注解
beanx10.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: context= "http://www.springframework.org/schema/context"
xmlns: aop= "http://www.springframework.org/schema/aop"
xsi: schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
< ! -- 扫描指定包-- >
< context: component- scan
base- package = "com.elf.spring.aop.homework" / >
< ! -- 启用基于注解的AOP 功能-- >
< aop: aspectj- autoproxy/ >
< / beans>
package com. elf. spring. aop. homework ;
public interface Cal {
public int cal1 ( int n) ;
public int cal2 ( int n) ;
}
package com. elf. spring. aop. homework ;
import org. springframework. stereotype. Component ;
@Component
public class MyCal implements Cal {
@Override
public int cal1 ( int n) {
int res = 1 ;
for ( int i = 1 ; i <= n; i++ ) {
res += i;
}
System . out. println ( "cal1 执行结果=" + res) ;
return res;
}
@Override
public int cal2 ( int n) {
int res = 1 ;
for ( int i = 1 ; i <= n; i++ ) {
res *= i;
}
System . out. println ( "cal2 执行结果=" + res) ;
return res;
}
}
package com. elf. spring. aop. homework ;
import org. aspectj. lang. JoinPoint ;
import org. aspectj. lang. Signature ;
import org. aspectj. lang. annotation. AfterReturning ;
import org. aspectj. lang. annotation. Aspect ;
import org. aspectj. lang. annotation. Before ;
import org. springframework. stereotype. Component ;
@Aspect
@Component
public class MyCalAOP {
@Before ( value = "execution(public int MyCal.*(int))" )
public void calStart ( JoinPoint joinPoint) {
Signature signature = joinPoint. getSignature ( ) ;
System . out. println ( signature. getName ( ) + " 执行, 开始执行时间=" + System . currentTimeMillis ( ) ) ;
}
@AfterReturning ( value = "execution(public int MyCal.*(int))" )
public void calEnd ( JoinPoint joinPoint) {
Signature signature = joinPoint. getSignature ( ) ;
System . out. println ( signature. getName ( ) + " 执行, 结束时间=" + System . currentTimeMillis ( ) ) ;
}
}
package com. elf. spring. aop. homework ;
import org. junit. jupiter. api. Test ;
import org. springframework. context. ApplicationContext ;
import org. springframework. context. support. ClassPathXmlApplicationContext ;
public class TestMyCalAOP {
@Test
public void testMyCalByAnnotation ( ) {
ApplicationContext ioc =
new ClassPathXmlApplicationContext ( "beans10.xml" ) ;
Cal cal = ioc. getBean ( Cal . class ) ;
cal. cal1 ( 10 ) ;
System . out. println ( "===========" ) ;
cal. cal2 ( 5 ) ;
}
}
第二种方式:基于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"
xsi: schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
< ! -- 配置MyCalAOP - bean id就用类名首字母小写-- >
< bean class = "com.elf.spring.aop.homework.xml.MyCalAOP" id= "myCalAOP" / >
< ! -- 配置MyCal - bean-- >
< bean class = "com.elf.spring.aop.homework.xml.MyCal" id= "myCal" / >
< ! -- 配置切面类-- >
< aop: config>
< ! -- 配置切入点表达式-- >
< aop: pointcut id= "myPointCut" expression= "execution(public int com.elf.spring.aop.homework.xml.MyCal.*(int))" / >
< ! -- 配置前置,返回-- >
< aop: aspect ref= "myCalAOP" order= "10" >
< aop: before method= "calStart" pointcut- ref= "myPointCut" / >
< aop: after- returning method= "calEnd" pointcut- ref= "myPointCut" / >
< / aop: aspect>
< / aop: config>
< / beans>
package com. elf. spring. aop. homework. xml ;
public interface Cal {
public int cal1 ( int n) ;
public int cal2 ( int n) ;
}
package com. elf. spring. aop. homework. xml ;
public class MyCal implements Cal {
@Override
public int cal1 ( int n) {
int res = 1 ;
for ( int i = 1 ; i <= n; i++ ) {
res += i;
}
System . out. println ( "cal1 执行结果=" + res) ;
return res;
}
@Override
public int cal2 ( int n) {
int res = 1 ;
for ( int i = 1 ; i <= n; i++ ) {
res *= i;
}
System . out. println ( "cal2 执行结果=" + res) ;
return res;
}
}
package com. elf. spring. aop. homework. xml ;
import org. aspectj. lang. JoinPoint ;
import org. aspectj. lang. Signature ;
public class MyCalAOP {
public void calStart ( JoinPoint joinPoint) {
Signature signature = joinPoint. getSignature ( ) ;
System . out. println ( signature. getName ( ) + " 基于XML配置- 执行, 开始执行时间=" + System . currentTimeMillis ( ) ) ;
}
public void calEnd ( JoinPoint joinPoint) {
Signature signature = joinPoint. getSignature ( ) ;
System . out. println ( signature. getName ( ) + " 基于XML配置- 执行, 结束时间=" + System . currentTimeMillis ( ) ) ;
}
}
package com. elf. spring. aop. homework. xml ;
import org. junit. jupiter. api. Test ;
import org. springframework. context. ApplicationContext ;
import org. springframework. context. support. ClassPathXmlApplicationContext ;
public class TestMyCalAOP {
@Test
public void testMyCalByXML ( ) {
ApplicationContext ioc =
new ClassPathXmlApplicationContext ( "beans11.xml" ) ;
Cal cal = ioc. getBean ( Cal . class ) ;
cal. cal1 ( 10 ) ;
System . out. println ( "===========" ) ;
cal. cal2 ( 5 ) ;
}
}