用异常通知捕获servlet的所有的方法抛出的异常:
意义:
代码
com.peng.aspect
MyAspect
package com.peng.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;/*** 切面* * @author Administrator* */
@Component
@Aspect
public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc()")public Object aroundSomething(ProceedingJoinPoint pjp) throws Throwable {System.out.println("环绕事务---------aa----------------");Object obj = pjp.proceed();System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}
com.peng.dao
PersonDao
package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}
com.peng.pojo
Person
package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component(value = "person")
public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
}
com.peng.service
PersonService
package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
import com.peng.pojo.Person;@Service(value = "personService")
public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}
com.peng.test
Test
package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
}
com.peng.web
PersonServlet
package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
import com.peng.service.PersonService;@Controller(value = "personServlet")
public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;public void savePerson(Person person) {int bug = 1 / 0;System.out.println(bug);personService.savePerson(person);}}
配置文件
控制台输出
意义:用来监控程序的性能问题
代码
com.peng.aspect
MyAspect
package com.peng.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;/*** 切面* * @author Administrator* */
@Component
@Aspect
public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc()")public Object aroundSomething(ProceedingJoinPoint pjp) throws Throwable {long startTime = System.currentTimeMillis();System.out.println("环绕事务---------aa--------当前时间(毫秒值):" + startTime);Object obj = pjp.proceed();long endTime = System.currentTimeMillis();System.out.println("环绕事务----------ww------当前时间(毫秒值):" + endTime);System.out.println("方法的执行时间:" + (endTime - startTime));return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}
com.peng.dao
PersonDao
package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}
com.peng.pojo
Person
package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component(value = "person")
public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
}
com.peng.service
PersonService
package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
import com.peng.pojo.Person;@Service(value = "personService")
public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}
com.peng.test
Test
package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
}
com.peng.web
PersonServlet
package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
import com.peng.service.PersonService;@Controller(value = "personServlet")
public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;public void savePerson(Person person) {personService.savePerson(person);}}
配置文件
代码
com.peng.annocation
Transaction
package com.peng.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Transaction {}
com.peng.aspect
MyAspect
package com.peng.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import com.peng.annotation.Transaction;/*** 切面* * @author Administrator* */
@Component
@Aspect
public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc() && @annotation(ann)")public Object aroundSomething(ProceedingJoinPoint pjp, Transaction ann)throws Throwable {System.out.println("环绕事务---------aa----------------");System.out.println("事务Start");Object obj = pjp.proceed();System.out.println("事务End");System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}
com.peng.dao
PersonDao
package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}
com.peng.pojo
Person
package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component(value = "person")
public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
}
com.peng.service
PersonService
package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
import com.peng.pojo.Person;@Service(value = "personService")
public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}
com.peng.test
Test
package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法(实验组)personServlet.savePerson(person);System.out.println("=8=8==8=8=8=8=8==8=8=8=8=8=8=8=8=8=8=8=8==8=8=8=8=8==88=8=8=");// 执行PersonServlet的delPerson方法(参照组)personServlet.delPerson();}
}
com.peng.web
PersonServlet
package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;import com.peng.annotation.Transaction;
import com.peng.pojo.Person;
import com.peng.service.PersonService;@Controller(value = "personServlet")
public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;@Transactionpublic void savePerson(Person person) {personService.savePerson(person);}public void delPerson() {System.out.println("del person ~~");}}
配置文件
代码
com.peng.annotation
Right
package com.peng.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 权限控制* * @author Administrator* */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Right {String right() default "游客";
}
com.peng.aspect
MyAspect
package com.peng.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import com.peng.annotation.Right;/*** 切面* * @author Administrator* */
@Component
@Aspect
public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc() && @annotation(right)")public Object aroundSomething(ProceedingJoinPoint pjp, Right right)throws Throwable {System.out.println("环绕事务---------aa----------------");System.out.println("当前用户权限:" + right.right());if (!"管理员".equals(right.right())) {System.out.println("e,你不是管理员,不能进行该操作");return null;}Object obj = pjp.proceed();System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}
com.peng.dao
PersonDao
package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}
com.peng.pojo
Person
package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component(value = "person")
public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
}
com.peng.service
PersonService
package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
import com.peng.pojo.Person;@Service(value = "personService")
public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}
com.peng.test
Test
package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
}
com.peng.web
PersonServlet
package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
import com.peng.service.PersonService;@Controller(value = "personServlet")
public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;// @Right(right = "管理员")@Rightpublic void savePerson(Person person) {personService.savePerson(person);}}
配置文件
代码
com.peng.annotation
CacheData
package com.peng.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 数据缓存自定义注解* * @author Administrator* */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheData {String name();
}
com.peng.aspect
DataCacheAspect
package com.peng.aspect;import java.util.HashMap;
import java.util.Map;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import com.peng.annotation.CacheData;
import com.sun.istack.internal.logging.Logger;/*** 数据缓存切面* * @author Administrator* */
@Component
@Aspect
public class DataCacheAspect {private Logger logger = Logger.getLogger(DataCacheAspect.class);private Map
}
MyAspect
package com.peng.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import com.peng.annotation.Right;/*** 切面* * @author Administrator* */
@Component
@Aspect
public class MyAspect {//// /**// * 给web层加入切面// */// 创建一个切入点表达式的引用@Pointcut("within(com.peng.web.*)")public void pc() {}// 前置事务@Before(value = "pc()")public void beforeSomething(JoinPoint jp) {System.out.println("前置事务");}//// 环绕事务@Around(value = "pc() && @annotation(right)")public Object aroundSomething(ProceedingJoinPoint pjp, Right right)throws Throwable {System.out.println("环绕事务---------aa----------------");Object obj = pjp.proceed();System.out.println("环绕事务----------ww--------------");return obj;}//// 后置事务@AfterReturning(value = "pc()")public void afterSomething(JoinPoint jp) {System.out.println("后置事务");}// 异常事务@AfterThrowing(value = "pc()", throwing = "t")public void exceptionSomething(JoinPoint jp, Throwable t) {System.out.println("异常事务");System.err.println("异常类:" + t.getClass());System.err.println("异常信息:" + t.getMessage());System.out.println("异常事务");}// 最终事务@After(value = "pc()")public void finallySomething(JoinPoint jp) {System.out.println("最终事务");}}
com.peng.dao
PersonDao
package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")
public class PersonDao {public void savePerson(Person person) {System.out.println("存储了对象" + person);}}
com.peng.pojo
Person
package com.peng.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component(value = "person")
public class Person {@Value(value = "张三")private String name;@Value(value = "12")private int age;@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}
}
com.peng.service
PersonService
package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;
import com.peng.pojo.Person;@Service(value = "personService")
public class PersonService {@Autowired@Qualifier(value = "personDao")private PersonDao personDao;public void savePerson(Person person) {personDao.savePerson(person);}}
com.peng.test
Test
package com.peng.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.peng.pojo.Person;
import com.peng.web.PersonServlet;/*** 测试通用类* * @author Administrator* */
public class Test {@org.junit.Testpublic void personModelTest() {// 获取Spring容器ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取Person对象Person person = (Person) cpxac.getBean("person");// 获取PersonServlet对象PersonServlet personServlet = (PersonServlet) cpxac.getBean("personServlet");// 执行PersonServlet的savePerson方法personServlet.savePerson(person);}
}
com.peng.web
PersonServlet
package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;import com.peng.pojo.Person;
import com.peng.service.PersonService;@Controller(value = "personServlet")
public class PersonServlet {@Autowired@Qualifier("personService")private PersonService personService;// @Right(right = "管理员")@Rightpublic void savePerson(Person person) {personService.savePerson(person);}}
配置文件