AOP:Spring的xml配置方式

<?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">
    
    <bean name="userManager" class="com.wangcf.manager.UserManagerImp"/>
    <bean name="logManager" class="com.wangcf.manager.aspect.LogManager"/>
    
    <aop:config>
        <aop:aspect id="logMgrAspect" ref="logManager">
            <aop:before method="addLogBefore" pointcut="execution(* com.wangcf.manager.*.add*(..))"/>
            <aop:after method="addLogAfter" pointcut="execution(* com.wangcf.manager.*.add*(..))"/>
        </aop:aspect>
    </aop:config>
</beans>
//切面类
package
com.wangcf.manager.aspect; import org.aspectj.lang.JoinPoint; public class LogManager { public static void addLogBefore(){ System.out.println("添加日志 Before..."); } public void addLogAfter(){ System.out.println("添加日志记录 After..."); } public void addLogAfterReturning(){ System.out.println("添加日志记录 AfterReturning..."); } public void addLogAfterThrowing(){ System.out.println("添加日志记录 AfterThrowing..."); } public void addLogAround(JoinPoint joinPoint){ System.out.println("添加日志记录 AfterAround start..."); System.out.println(joinPoint.getTarget()); System.out.println("添加日志记录 AfterAround end..."); } }

测试类:

package com.wangcf.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wangcf.manager.IUserManager;
import com.wangcf.po.User;


public class TestAction {
    public static void main(String[] args) {
        BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserManager userManager=(IUserManager) factory.getBean("userManager");
        //输出代理
        System.out.println(userManager.getClass().getName());
        userManager.addUser();
        System.out.println("=================");
    }
}

输出:

 

posted @ 2017-01-24 15:06  言凡  阅读(280)  评论(0编辑  收藏  举报