欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Aop 日志管理

程序员文章站 2022-03-02 15:39:43
...

Spring Aop 日志管理

aop

—— <a href="" target="_blank"> [ * ]

代码块

1、 定义并实现对日志方面的处理。

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;


        @Override
        public void before(Method method1, Object[] arg1, Object arg2) throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("   ");
            System.out.println("前置系统日志:"+"当前时间:"+(new Date())+":"+"调用了"+method1.getName()+" :使用了参数"+(Arrays.toString(arg1)));  
        }

        @Override
        public void afterReturning(Object arg0, Method method, Object[] args, Object arg2) throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("后置系统日志:"+"返回信息:"+arg0+" "+"当前时间:"+(new Date())+":"+
            "调用了"+method.getName()+" :使用了参数"+(Arrays.toString(args)));  

        }
}

2、定义业务代码1

public interface UserService {

    public void add(String name,String password);

    public void update(Integer id);
}
public class UserServiceimpl implements UserService{

    @Override
    public void add(String name,String password) {
        // TODO Auto-generated method stub
        System.out.println("注册--添加"+name +"   "+password);
    }

    @Override
    public void update(Integer id) {
        // TODO Auto-generated method stub
        System.out.println("修改");
    }

}

2、定义业务代码2

public interface IAdminService {
    public boolean login(String userName, String password);
}
public class AdminService implements IAdminService{

    @Override
    public boolean login(String userName, String password) {
        System.out.println("login:" + userName );
        return true;
    }

}

3、配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/data/mongo
                        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="logService" class="springAop.log.LogService"/> <!--处理日志方面的bean-->  
    <bean id="adminService" class="springAop.log.AdminService"/><!--处理业务的bean-->  
    <bean id="userService" class="springAop.log.UserServiceimpl"/><!--处理业务的bean-->  


    <!--将日志bean和业务bean通过代理的方式进行代理整合-->  

        <!-- 单个业务处理日志管理 -->
        <!-- <bean name="aopmethod" class="org.springframework.aop.framework.ProxyFactoryBean">  
            <property name="interceptorNames">  
               <list>  
                  <value>logService</value>  
                      此处还可以放置多个处理方面的bean  
               </list>  
           </property>  
           <property name="target" ref="adminService"></property>  
        </bean>    -->

      <!-- 多个业务处理日志管理 -->
        <bean name="logmanager" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
            <property name="interceptorNames">  
               <list>  
                  <value>logService</value>
                   <!--    此处还可以放置多个处理方面的bean   -->
               </list>  
           </property>  
           <property name="beanNames">
            <list>
                <value>adminService</value>
                <value>userService</value>
            </list>
           </property>  
        </bean>  
</beans>

4.测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

    public static void testSpringAOP(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        IAdminService iAdminService =(IAdminService) applicationContext.getBean("adminService");
        iAdminService.login("huahua", "123456");
        UserService userService =(UserService) applicationContext.getBean("userService");
        userService.add("huahua1", "123456");
        userService.update(1);
    }
        public static void main(String[] args) {
            TestMain.testSpringAOP();
        }
}

运行结果:

前置系统日志:当前时间:Wed May 03 16:17:41 CST 2017:调用了login :使用了参数[huahua, 123456]
login:huahua
后置系统日志:返回信息:true 当前时间:Wed May 03 16:17:41 CST 2017:调用了login :使用了参数[huahua, 123456]

前置系统日志:当前时间:Wed May 03 16:17:41 CST 2017:调用了add :使用了参数[huahua1, 123456]
注册--添加huahua1   123456
后置系统日志:返回信息:null 当前时间:Wed May 03 16:17:41 CST 2017:调用了add :使用了参数[huahua1, 123456]

前置系统日志:当前时间:Wed May 03 16:17:41 CST 2017:调用了update :使用了参数[1]
修改
后置系统日志:返回信息:null 当前时间:Wed May 03 16:17:41 CST 2017:调用了update :使用了参数[1]