- 浏览: 989124 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1441)
- 软件思想&演讲 (9)
- 行业常识 (250)
- 时时疑问 (5)
- java/guava/python/php/ruby/R/scala/groovy (213)
- struct/spring/springmvc (37)
- mybatis/hibernate/JPA (10)
- mysql/oracle/sqlserver/db2/mongdb/redis/neo4j/GreenPlum/Teradata/hsqldb/Derby/sakila (268)
- js/jquery/jqueryUi/jqueryEaseyUI/extjs/angulrJs/react/es6/grunt/zepto/raphael (81)
- ZMQ/RabbitMQ/ActiveMQ/JMS/kafka (17)
- lucene/solr/nuth/elasticsearch/MG4J (167)
- html/css/ionic/nodejs/bootstrap (19)
- Linux/shell/centos (56)
- cvs/svn/git/sourceTree/gradle/ant/maven/mantis/docker/Kubernetes (26)
- sonatype nexus (1)
- tomcat/jetty/netty/jboss (9)
- 工具 (17)
- ETL/SPASS/MATLAB/RapidMiner/weka/kettle/DataX/Kylin (11)
- hadoop/spark/Hbase/Hive/pig/Zookeeper/HAWQ/cloudera/Impala/Oozie (190)
- ios/swift/android (9)
- 机器学习&算法&大数据 (18)
- Mesos是Apache下的开源分布式资源管理框架 (1)
- echarts/d3/highCharts/tableau (1)
- 行业技能图谱 (1)
- 大数据可视化 (2)
- tornado/ansible/twisted (2)
- Nagios/Cacti/Zabbix (0)
- eclipse/intellijIDEA/webstorm (5)
- cvs/svn/git/sourceTree/gradle/jira/bitbucket (4)
- jsp/jsf/flex/ZKoss (0)
- 测试技术 (2)
- splunk/flunm (2)
- 高并发/大数据量 (1)
- freemarker/vector/thymeleaf (1)
- docker/Kubernetes (2)
- dubbo/ESB/dubboX/wso2 (2)
最新评论
Spring对AOP的实现提供了很好的支持。下面我们就使用Spring的注解来完成AOP做一个例子。
首先,为了使用Spring的AOP注解功能,必须导入如下几个包。aspectjrt.jar,aspectjweaver.jar,cglib-nodep.jar.
1、实体bean
Java代码 收藏代码
public class Person {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、接口类
Java代码 收藏代码
public interface PersonService {
public void save(Person person);
public void update(Person person);
public Person getByIdPerson(Long id);
public void delete(Long id);
}
3、实现类
Java代码 收藏代码
public class PersonServiceImpl implements PersonService{
Map<Long, Person> maps = new HashMap<Long, Person>();
@Override
public void save(Person person) {
System.out.println("***执行save方法***");
maps.put(person.getId(), person);
}
@Override
public void update(Person person) {
System.out.println("***执行update()方法***");
maps.remove(person.getId());
maps.put(person.getId(), person);
}
@Override
public Person getByIdPerson(Long id) {
System.out.println("***执行getByIdPerson()方法***");
return maps.get(id);
}
@Override
public void delete(Long id) {
System.out.println("***执行delete()方法***");
maps.remove(id);
}
}
4、使用Spring注解方式对这个Bean进行方法拦截
Java代码 收藏代码
@Aspect
public class MyInterceptor {
@Pointcut("execution(* cn.tzz.aop.annotation.service.impl..*.*(..))")
private void anyMethod(){}//定义切点
@Before("anyMethod() && args(person)")
public void doAccessCheck(Person person){
System.out.println(person.getName());
System.out.println("前置通知");
}
@AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
}
@After("anyMethod()")
public void after(){
System.out.println("最终通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("异常通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("进入环绕通知");
Object object = proceedingJoinPoint.proceed();//执行该方法
System.out.println("退出方法");
return object;
}
}
Java代码 收藏代码
@Pointcut("execution(* cn.tzz.aop.annotation.service.impl..*.*(..))")
上述是定义方法切入点,execution为执行的意思,*代表任意返回值,然后是包名,.*意思是包下面的所有子包,(..)代表各种方法.
5、在Spring的配置文件中配置Bean,需要打开AOP命名空间
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="cn.tzz.aop.annotation" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="personService" class="cn.tzz.aop.annotation.service.impl.PersonServiceImpl"></bean>
<bean id="myInterceptor" class="cn.tzz.aop.annotation.MyInterceptor"></bean>
</beans>
6、测试
Java代码 收藏代码
public class TestAop {
private static ApplicationContext ctx = null;
private static PersonService personService = null;
static{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
personService = (PersonService)ctx.getBean("personService");
}
public void testSave(){
Person person = new Person();
person.setId(1L);
person.setName("abc");
personService.save(person);
}
public void testGetByIdPerson(){
Person p = personService.getByIdPerson(1L);
System.out.println(p.getId()+"---"+p.getName());
}
public void testUpdate(){
Person person = new Person();
person.setId(1L);
person.setName("abc_1");
personService.update(person);
}
public void testDelete(){
personService.delete(1L);
}
@Test
public void testInteceptor(){
testSave();
// testGetByIdPerson();
// testUpdate();
// testGetByIdPerson();
// testDelete();
// testGetByIdPerson();
}
}
7、测试结果
Java代码 收藏代码
abc
前置通知
进入环绕通知
***执行save方法***
后置通知
退出方法
最终通知
首先,为了使用Spring的AOP注解功能,必须导入如下几个包。aspectjrt.jar,aspectjweaver.jar,cglib-nodep.jar.
1、实体bean
Java代码 收藏代码
public class Person {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、接口类
Java代码 收藏代码
public interface PersonService {
public void save(Person person);
public void update(Person person);
public Person getByIdPerson(Long id);
public void delete(Long id);
}
3、实现类
Java代码 收藏代码
public class PersonServiceImpl implements PersonService{
Map<Long, Person> maps = new HashMap<Long, Person>();
@Override
public void save(Person person) {
System.out.println("***执行save方法***");
maps.put(person.getId(), person);
}
@Override
public void update(Person person) {
System.out.println("***执行update()方法***");
maps.remove(person.getId());
maps.put(person.getId(), person);
}
@Override
public Person getByIdPerson(Long id) {
System.out.println("***执行getByIdPerson()方法***");
return maps.get(id);
}
@Override
public void delete(Long id) {
System.out.println("***执行delete()方法***");
maps.remove(id);
}
}
4、使用Spring注解方式对这个Bean进行方法拦截
Java代码 收藏代码
@Aspect
public class MyInterceptor {
@Pointcut("execution(* cn.tzz.aop.annotation.service.impl..*.*(..))")
private void anyMethod(){}//定义切点
@Before("anyMethod() && args(person)")
public void doAccessCheck(Person person){
System.out.println(person.getName());
System.out.println("前置通知");
}
@AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
}
@After("anyMethod()")
public void after(){
System.out.println("最终通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("异常通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("进入环绕通知");
Object object = proceedingJoinPoint.proceed();//执行该方法
System.out.println("退出方法");
return object;
}
}
Java代码 收藏代码
@Pointcut("execution(* cn.tzz.aop.annotation.service.impl..*.*(..))")
上述是定义方法切入点,execution为执行的意思,*代表任意返回值,然后是包名,.*意思是包下面的所有子包,(..)代表各种方法.
5、在Spring的配置文件中配置Bean,需要打开AOP命名空间
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="cn.tzz.aop.annotation" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="personService" class="cn.tzz.aop.annotation.service.impl.PersonServiceImpl"></bean>
<bean id="myInterceptor" class="cn.tzz.aop.annotation.MyInterceptor"></bean>
</beans>
6、测试
Java代码 收藏代码
public class TestAop {
private static ApplicationContext ctx = null;
private static PersonService personService = null;
static{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
personService = (PersonService)ctx.getBean("personService");
}
public void testSave(){
Person person = new Person();
person.setId(1L);
person.setName("abc");
personService.save(person);
}
public void testGetByIdPerson(){
Person p = personService.getByIdPerson(1L);
System.out.println(p.getId()+"---"+p.getName());
}
public void testUpdate(){
Person person = new Person();
person.setId(1L);
person.setName("abc_1");
personService.update(person);
}
public void testDelete(){
personService.delete(1L);
}
@Test
public void testInteceptor(){
testSave();
// testGetByIdPerson();
// testUpdate();
// testGetByIdPerson();
// testDelete();
// testGetByIdPerson();
}
}
7、测试结果
Java代码 收藏代码
abc
前置通知
进入环绕通知
***执行save方法***
后置通知
退出方法
最终通知
发表评论
-
Spring Boot
2017-09-26 09:51 249Spring Boot是由Pivotal团 ... -
spring AOP中切点
2017-08-25 09:59 859在spring AOP中,需要使用AspectJ的切点表达式 ... -
Spring JdbcTemplate详解
2017-07-19 16:12 555JdbcTemplate简介 Spring对数据库的操 ... -
Spring相关
2017-04-20 16:10 427我是一个Java开发者,之前知道Spring属于这个公司,就 ... -
spring.schemas、spring.handlers的使用
2017-02-28 13:55 1674报错信息:Configuration problem: Un ... -
Spring的一个命名空间的名称空间处理程序没有找到
2017-02-25 15:20 9011. 问题 本文将讨论Spring中最常见的配置问题 —— ... -
到底EJB是什么
2016-12-06 10:05 416到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得 ... -
Spring MVC 和 Servlet 一样,都不是线程安全的
2016-04-28 01:06 623你的理解是对的,Spring MVC 和 Servlet 一 ... -
springmvc的control 的线程是否安全的问题
2016-05-31 10:09 323关于java Servlet,Struts,springM ... -
框架的一些学习
2016-02-03 14:53 220java aopalliance-1.0.jar这个包是AOP ... -
Metrics介绍和Spring的集成
2016-02-02 16:56 898Metrics可以为你的代码的运行提供无与伦 ... -
spring mvc常用的注解
2016-01-22 14:28 660spring mvc常用的注解: ... -
Spring的常用注解
2016-01-20 16:07 606Spring2.5引入注解式处理器 @Controlle ... -
通过Spring的配置,添加多个数据源,制作多个qlMapClient,iBatis或Hibernate的各个DAO
2016-12-27 10:25 475通过Spring的配置,添加多个数据源,制作多个qlMapCl ... -
springmvc避免IE执行AJAX时,返回JSON出现下载文件
2017-01-01 23:35 821<!-- 避免IE执行AJAX时,返回JSON出现下载文 ... -
springMVC的@ResponseBody出现乱码解决方法
2017-01-02 00:23 342使用@ResponseBody出现乱码解决方法 1、 Re ... -
Spring中的Bean的注入方式
2017-01-02 00:27 501一 setter方法注入 配置文件如下: <bean ... -
spring发送邮件配置文件
2017-01-02 00:27 12361、发送邮件配置文件springmail_config ... -
@Resource和@Autowire的区别
2017-01-02 00:27 661@Resource和@Autowire的区别 在java代码中 ... -
Spring中继承并简化了Quartz
2017-01-02 00:27 634Quartz是一个强大的企业级任务调度框架,Spring中继承 ...
相关推荐
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,AOP的原理就是java的动态代理机制
使用Spring的注解方式实现AOP的细节
spring aop 通过注解实现的项目,这只是一个简单的例子。运行环境也简单。
演示了spring对aop的支持,包括注解方式、基于xml方式。
SpringAOP注解方式.zip,SpringAOP注解特棒例子
spring AOP,注解方式的实现! 内部方法的拦截
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
spring 注解 aop demo spring 注解 aop demo spring 注解 aop demo spring 注解 aop demo
最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
代码中有注解实例,spring框架实例,aop切片实例
spring aop注解所需要的三个jar包,aspectjrt.jar,aspectjweaver.jar,aopalliance.jar
spring aop样例.方便对springaop的了解和快速学习
spring-aop注解用到的jar包,解压后直接导入即可使用。
先引入Spring框架开发的基本开发包,再引入Spring框架的AOP的开发包
Spring 框架中对面向切面编程aop的理解,java中对于spring的理解是有助于java的学习
spring aop详解,详细地讲述了spring中aop的实现,实现原理,实现过程。