Spring的AOP基於xml常用的幾種配置
一般spring的AOP實現方式有二中
1、spring-aop自己實現與IOC結合使用,也是我們最常用的方式
2、使用AspectJ,即註解的方式
個人覺得,spring中使用配置檔案還是比較簡潔的。
spring中要想使用aop這種方式,需要以下jar包:
首先,如果要在工程中使用AOP需要幾個jar包:
1 Aop的核心包,即org.springframework.aop-xxx.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");
}
}