一般spring的AOP实现方式有二中
1、spring-aop自己实现与IOC结合使用,也是我们最常用的方式
2、使用AspectJ,即注解的方式
个人觉得,spring中使用配置文件还是比较简洁的。
spring中要想使用aop这种方式,需要以下jar包:
首先,如果要在工程中使用AOP需要几个jar包:
1 Aop的核心包,即org.springframework.aop-xxx.jar
2 Spring的联盟包:aopalliance-1.0.jar
3 aspectJ相关的jar包:aspectjrt.jar aspectjweaver.jar
4 如果使用了动态代理,还需要添加cglib相关的jar包:cglib.zip
接下来是使用AOP将我们自己的切面,编织到相应的切点处applicationContext.xml具体的配置
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd ">
<bean id="logAspect" class="aop.LogAspect"></bean>
<bean id="userService" class="service.UserServiceImpl"></bean>
<bean id="personService" class="service.PersonServiceImpl">
<property name="persondao" ref="persondao"></property>
</bean>
<bean id="persondao" class="dao.PersonDAOImpl"></bean>
<!-- 一般的配置使用方法 -->
<!--
<aop:config>
<aop:aspect ref="logAspect">
<aop:pointcut id="allManagerMethod" expression="execution(* service.*.*(..))"></aop:pointcut>
<aop:before method="writeLog" pointcut-ref="allManagerMethod"></aop:before>
<aop:after method="writeLogToDb" pointcut-ref="allManagerMethod"></aop:after>
<aop:around method="writeLogToDisk" pointcut-ref="allManagerMethod"></aop:around>
</aop:aspect>
</aop:config>
-->
<!-- 常用的配置方式,配置自己实现的拦截器方法 -->
<!-- 对于所有pointcut指定的类,都使用拦截器进行先拦截,然后在执行这些service的服务 -->
<bean id="timeInterceptor" class="methodInterceptor.TimeInterceptor"></bean>
<!--
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* service.*.*(..))"></aop:pointcut>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="timeInterceptor"></aop:advisor>
</aop:config>
-->
<!-- spring加载容器中指定的类,使用拦截器拦截他们 -->
<bean id="daoMonitorProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<value>timeInterceptor</value>
</property>
</bean>
</beans>
package application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PersonService;
import service.UserService;
import dao.PersonDAO;
public class AopApplicationContextTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("aopapplicationContext.xml");
UserService userService=(UserService)context.getBean("userService");
userService.login();
PersonService personService=(PersonService)context.getBean("personService");
personService.eat();;
PersonDAO personDAO=(PersonDAO)context.getBean("persondao");
personDAO.insert();
}
}
package dao;
public interface PersonDAO {
public void insert();
public void delete();
}
package dao;
public class PersonDAOImpl implements PersonDAO{
@Override
public void insert() {
System.out.println("insert person");
}
@Override
public void delete() {
System.out.println("delete peron");
}
}
package service;
public interface UserService {
public void login();
}
package service;
public class UserServiceImpl implements UserService {
@Override
public void login() {
System.out.println("user loginging");
}
}
package aop;
public class LogAspect {
public void writeLog(){
System.out.println("start to write log");
}
public void writeLogToDb(){
System.out.println("start to write log to DB");
}
public void writeLogToDisk(){
System.out.println("start to write log to Disk");
}
}