Spring的AOP基于xml常用的几种配置

一般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");
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值