总结自己对Spring AOP的理解
来源:互联网 发布:windows镜像怎么安装 编辑:程序博客网 时间:2024/04/29 08:03
总结自己对Spring AOP的理解
以前在学校的时候也学习了一些关于当前主流的三大框架,Struts2.0,Hibernate,Spring。尤其是对于Spring的学习,感觉像是一层雾水。来到公司之后,公司又要用到Spring框架。其为企业提供了轻量级的解决方案,尤其是在事物管理和一些日志管理方面,其相对于EJB3.0来说更加方便和快捷。尤其对于Spring中的AOP(其也是Spring框架的核心之一,我自己觉得Spring框架的精华也就是IOC(DI)和AOP),其中AOP(Aspect Oritented Programming)面向切面编程。
在日常的开发中我们可能需要对一些业务逻辑的处理需要增强。例如对于数据库操作的时候我们需要使用事物来commit()一下之前的操作方法。也即是对方法(例如save)进行了增强。所谓AOP就是可以对接入点(Jointcut)进行预处理和后期处理,当接入点进入切面的时候我们可以对其进行同意的处理。这就是我对AOP的理解。
首先,我们需要自己定义一个切面Aspect,用于对接入点的处理,例子中的Aspect为AdviceAspect.
下面给出一个简单的例子:代码
<?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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- aop的配置 -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect id="TestAspect" ref="adviceAspect">
<!-- jointcut配置 -->
<aop:pointcut expression="execution(* com.clark.spring.aop.service.*.say*(..))"
id="businesss"/>
<aop:before method="before" pointcut-ref="businesss" />
<aop:around method="around" pointcut-ref="businesss"/>
<aop:after method="after" pointcut-ref="businesss"/>
</aop:aspect>
</aop:config>
<!-- 切面 -->
<bean id="adviceAspect" class="com.clark.spring.aop.AdviceAspect"></bean>
<!-- 实现bean -->
<bean id="userServiceImpl" class="com.clark.spring.aop.service.impl.UserServiceImpl"></bean>
</beans>
package com.clark.spring.aop.service;
/**
* service接口
* @author Administrator
*
*/
public interface UserService {
public String sayHello(String name);
public void call();
}
package com.clark.spring.aop.service.impl;
import com.clark.spring.aop.service.UserService;
/**
* 业务实现类
* @author Administrator
*
*/
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return name;
}
@Override
public void call() {
System.out.println("call me");
}
}
package com.clark.spring.aop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.clark.spring.aop.service.UserService;
//测试类
public class UserTest {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
UserService service=(UserService)context.getBean("userServiceImpl");
//调用sayHello("world")将会被Aspect切面增强
System.out.println(service.sayHello(" world"));
//调用call()方法将不会被增强
service.call();
}
}
在日常的开发中我们可能需要对一些业务逻辑的处理需要增强。例如对于数据库操作的时候我们需要使用事物来commit()一下之前的操作方法。也即是对方法(例如save)进行了增强。所谓AOP就是可以对接入点(Jointcut)进行预处理和后期处理,当接入点进入切面的时候我们可以对其进行同意的处理。这就是我对AOP的理解。
首先,我们需要自己定义一个切面Aspect,用于对接入点的处理,例子中的Aspect为AdviceAspect.
下面给出一个简单的例子:代码
<?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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- aop的配置 -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect id="TestAspect" ref="adviceAspect">
<!-- jointcut配置 -->
<aop:pointcut expression="execution(* com.clark.spring.aop.service.*.say*(..))"
id="businesss"/>
<aop:before method="before" pointcut-ref="businesss" />
<aop:around method="around" pointcut-ref="businesss"/>
<aop:after method="after" pointcut-ref="businesss"/>
</aop:aspect>
</aop:config>
<!-- 切面 -->
<bean id="adviceAspect" class="com.clark.spring.aop.AdviceAspect"></bean>
<!-- 实现bean -->
<bean id="userServiceImpl" class="com.clark.spring.aop.service.impl.UserServiceImpl"></bean>
</beans>
package com.clark.spring.aop.service;
/**
* service接口
* @author Administrator
*
*/
public interface UserService {
public String sayHello(String name);
public void call();
}
package com.clark.spring.aop.service.impl;
import com.clark.spring.aop.service.UserService;
/**
* 业务实现类
* @author Administrator
*
*/
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return name;
}
@Override
public void call() {
System.out.println("call me");
}
}
package com.clark.spring.aop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.clark.spring.aop.service.UserService;
//测试类
public class UserTest {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
UserService service=(UserService)context.getBean("userServiceImpl");
//调用sayHello("world")将会被Aspect切面增强
System.out.println(service.sayHello(" world"));
//调用call()方法将不会被增强
service.call();
}
}
- 总结自己对Spring AOP的理解
- 对spring AOP 的理解
- 对spring的aop理解
- 对spring AOP的理解
- 对spring aop的理解
- 对spring aop的理解
- 我对Spring的aop的理解
- Spring 中对IOC,AOP的理解
- Spring AOP的实现——对AOP的理解
- 对AOP的理解
- 对Spring的AOP的浅薄的理解
- 自己对switch 语句的理解总结
- spring aop的理解
- Spring AOP的理解
- spring 的AOP 理解
- Spring AOP的理解
- Spring AOP的理解
- 对spring的DI,IOC, AOP概念的理解
- property和constructor-arg的使用
- ConfigHelper 配置文件辅助类
- 集合的排序和利用LINQ的查找
- 为什么我的电脑总是提示内存不足?
- 夫妻、情侣必看,《半个西瓜》
- 总结自己对Spring AOP的理解
- android开发步步为营之3:自定义控件之手势签名
- epoll原理
- temp 表空间引发的等待及相关知识
- 写一个函数求一个整数数组中第二大元素。
- Apache与Tomcat 区别联系
- 《把时间当朋友》 读书笔记 困境 醒悟
- 从玩扑克到软件开发
- android开发步步为营之2:开发自定义进度条对话框