1.学添加包现将用到的包都放到这:
2.beans.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
http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<
bean
id
=
"studentserviceaspect"
class
=
"com.lidongyang.advice.StudentServiceAspect"
></
bean
>
<
bean
id
=
"studentservice"
class
=
"com.lidongyang.service.impl.StudentServiceImpl"
></
bean
>
<
aop:config
>
<
aop:aspect
id
=
"studentserviceaspect"
ref
=
"studentserviceaspect"
>
<
aop:pointcut
expression
=
"execution(* com.lidongyang.service.*.*(..))"
id
=
"businessservice"
/>
<
aop:before
method
=
"doBefore"
pointcut-ref
=
"businessservice"
/>
<
aop:after
method
=
"doAfter"
pointcut-ref
=
"businessservice"
/>
<
aop:around
method
=
"doAround"
pointcut-ref
=
"businessservice"
/>
<
aop:after-returning
method
=
"doAfterReturn"
pointcut-ref
=
"businessservice"
/>
<
aop:after-throwing
method
=
"doAfterExcept"
pointcut-ref
=
"businessservice"
throwing
=
"ex"
/>
</
aop:aspect
>
</
aop:config
>
</
beans
>
3.相对应的类
H:\lianxi\java web\Spring403-01\src\com\lidongyang\advice
import
java.awt.print.Printable;
import
org.aspectj.lang.JoinPoint;
import
org.aspectj.lang.ProceedingJoinPoint;
public
class
StudentServiceAspect {
private
void
doBefore(JoinPoint jp) {
System.
out
.println(
"类名"
+jp.getTarget().getClass().getName());
System.
out
.println(
"方法名"
+jp.getSignature().getName());
System.
out
.println(
"添加学生姓名名"
+jp.getArgs()[0]);
}
private
void
doAfter(JoinPoint jp){
System.
out
.println(
"学生"
+jp.getArgs()[0]+
"添加完成"
);
}
private
Object doAround(ProceedingJoinPoint pjp)
throws
Throwable{
System.
out
.println(
"环绕开始"
);
Object retval=pjp.proceed();
System.
out
.println(
"环绕结束"
);
return
retval;
}
private
void
doAfterReturn(JoinPoint jp){
System.
out
.print(
"fanhuizhi"
);
}
private
void
doAfterExcept(JoinPoint jp,Throwable ex){
System.
out
.print(
"出现异常:"
);
System.
out
.println(ex.getMessage());
}
}
package
com.lidongyang.service;
public
interface
StudentService {
public
void
addStudent(String name);
}
package
com.lidongyang.service.impl;
import
com.lidongyang.service.StudentService;
public
class
StudentServiceImpl
implements
StudentService{
@Override
public
void
addStudent(String name) {
System.
out
.println(
"正在添加学生"
+name);
System.
out
.print(1/0);;
}
}
package
com.lidongyang.test;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
com.lidongyang.service.StudentService;
public
class
Test {
public
static
void
main(String[] args) {
ApplicationContext ac=
new
ClassPathXmlApplicationContext(
"beans.xml"
);
StudentService studentService=(StudentService)ac.getBean(
"studentservice"
);
studentService.addStudent(
"张三"
);
}
}