面向切面编程AOP的浅显理解(三)
来源:互联网 发布:asp淘宝客源码 编辑:程序博客网 时间:2024/05/22 06:19
前两篇都是简单介绍了相关的入门级别的概念,编程最重要的是需要动手,只知道概念是没有用的,需要动手,需要动手,需要动手!!!下面我就来简单介绍下AOP是如何使用的。
AOP这种思想在Spring里面有,在AspectJ里面也有,当然在Jboss里面也存在,基本上Spring和AspectJ代表了AOP的两种配置方式,即注解方式和XML配置方式。
一.注解的配置方式
注解的配置方式可以分为以下步骤:
1.书写核心业务逻辑代码;
2.添加切面和切入点以及通知这三个的注解;
3.配置XML文件。
示例代码:
先定义一个基础类:
publicclassReferenceAOP {
publicintreferenceTimes;
public Stringname;
publicintgetReferenceTimes() {
returnreferenceTimes;
}
publicvoidsetReferenceTimes(int referenceTimes) {
this.referenceTimes =referenceTimes;
}
public StringgetName(){
returnname;
}
publicvoidsetName(String name) {
this.name = name;
}
}
定义一个Dao接口:
publicinterfaceReferenceAOPDao {
publicvoidOutputReference();
}
定义一个实现类:
publicclassReferenceAOPDaoImplimplements ReferenceAOPDao {
@Override
publicvoid OutputReference() {
// TODO Auto-generated method stub
System.out.println("Test outputReference");
}
}
定义一个操作类:
publicclassReferenceAOPService {
private ReferenceAOPDaoreDao;
public ReferenceAOPDao getReDao() {
returnreDao;
}
publicvoidsetReDao(ReferenceAOPDaoreDao) {
this.reDao = reDao;
}
publicvoidoutPut(ReferenceAOPDao reDao) {
reDao.OutputReference();
}
}
测试代码:
public class TestAOPReference {
public void main(String args[]) throws Exception {
ClassPathXmlApplicationContext ctx = newClassPathXmlApplicationContext("applicationContext.xml");
ReferenceAOPService service = (ReferenceAOPService)ctx.getBean("referenceAOPService");
System.out.println(service.getClass());
service.outPut(new ReferenceAOPDao());
System.out.println("end");
}
XML配置文件:
<context:annotation-config/>
<context:component-scanbase-package="TestAOP"/> <!--自动扫描相关的类-->
<aop:aspectj-autoproxy/>
<beanid="referenceAOPService"class="TestAOP.ReferenceAOPService">
<propertyname="referenceAOPDao">
<refbean="referenceAOPDao"/>
</property>
</bean>
<beanid="referenceAOPDao"class="TestAOP.ReferenceAOPDaoImpl">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>
在这个配置文件里面一个很重要的点是:Spring是否支持注解的AOP是由该配置文件控制的,也就是<aop:aspectj-autoproxy/> ,如果在配置文件中声明了这一配置的时候,Spring就会支持注解的AOP。
二.XML的配置方式:在注解的配置方式的基础上增加一个如下的配置:其他没什么变化,XML配置方式的配置可以分为以下步骤:
1.配置指定的切面。
2.配置指定的切点;
3.配置指定的通知。
<aop:config>
<aop:pointcutexpression="execution(public* TestAOP..*.add(..))"
id="servicePointcut"/>
<aop:aspectid="logAspect"ref="AOPUtil">
<aop:before method="beforeMethod" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
参考博客:http://www.cnblogs.com/flowwind/p/4782606.html
- 面向切面编程AOP的浅显理解(三)
- 面向切面编程AOP的浅显理解(一)
- 面向切面编程AOP的浅显理解(二)
- 面向切面编程AOP的浅显理解(一)
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 面向切面编程(AOP)的理解
- 【gitlab】—项目实践中常见命令(一)
- 插入排序
- Linux 下Java和Android 环境变量设置
- 阿里对象存储OSS在laravel框架中的使用
- 【SQL】mysql语句整理(一)
- 面向切面编程AOP的浅显理解(三)
- CSU1562 Fun House
- Git(二)
- [kuangbin带你飞]专题二 搜索进阶 D
- 懵懂oracle之存储过程3--JOB详解
- Kali Linux GRUB2引导修复
- 【SpringMVC】自定义拦截器
- 让C程序更有效的十种方法
- ios-矩形-圆角矩形-椭圆-圆形的绘制