目录











- public class AOPAdvice {
- public void before(){
- System.out.println("before");
- }
- public void after(){
- System.out.println("after");
- }
- public void afterReturing(){
- System.out.println("afterReturing");
- }
- public void afterThrowing(){
- System.out.println("afterThrowing");
- }
- public void around(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("around before");
- //对原始方法的调用
- pjp.proceed();
- System.out.println("around after");
- }
- }
- <aop:aspect ref="myAdvice">
-
-
-
-
-
- <aop:around method="around" pointcut-ref="pt"/>
- aop:aspect>












- public class UserServiceImpl implements UserService {
- public void save(int i){
- //0.将共性功能抽取出来
- //System.out.println("共性功能");
- System.out.println("user service running..."+i);
- //int i=1/0;
- }
-
- @Override
- public int update() {
- System.out.println("user service update running...");
- return 100;
- }
-
- @Override
- public void delete() {
- System.out.println("user service delete running...");
- int i = 1/0;
- }
- }
- <aop:aspect ref="myAdvice">
-
-
-
-
- <aop:after-throwing method="afterThrowing" pointcut-ref="pt" throwing="t"/>
- <aop:around method="around" pointcut-ref="pt"/>
- aop:aspect>
- public class AOPAdvice {
- public void before(JoinPoint jp){
- Object[] args = jp.getArgs();
- System.out.println("before"+args[0]);
- }
- public void after(JoinPoint jp){
- Object[] args = jp.getArgs();
- System.out.println("after"+args[0]);
- }
- public void afterReturing(Object ret){
- System.out.println("afterReturing..."+ret);
- }
- public void afterThrowing(Throwable t){
- System.out.println("afterThrowing..."+t.getMessage());
- }
- public Object around(ProceedingJoinPoint pjp){
- System.out.println("around before");
- //对原始方法的调用
- Object ret = null;
- try {
- ret = pjp.proceed();
- } catch (Throwable e) {
- System.out.println("around...exception..."+e.getMessage());
- }
- System.out.println("around after..."+ret);
- return ret;
- }
- }
- public class App {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- UserService userService = (UserService) ctx.getBean("userService");
- //userService.save(666);
- //int tj = userService.update();
- //System.out.println("app......"+tj);
- userService.delete();
- }
- }