总结自己对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();
}
}