spring AOP 日志配置

1.通知import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogTest implements MethodInterceptor{

static Logger logger=Logger.getLogger(LogTest.class);

 
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
logger.setLevel(Level.INFO);
logger.log(Level.INFO,"before log");
logger.info("log test info before");
logger.error("log test before");
System.out.println("syso==before log");
Object result=mi.proceed();
System.out.println("syso==after log");
logger.log(Level.INFO,"after log");
return result;
}
public void test(){
System.out.println("test init");
}
public static void main(String[] args) {
LogTest logTest=new LogTest();
logger.info("main logger test");
System.out.println("TEST SYSO");
}
}

2.切入点

import com.opensymphony.xwork2.ActionContext;
public class SystemEntryAction {
    //入口
    public String redirectToOfficeSupplyPage(){
    UserForm userForm = userDetailUtil.getUserDetailFromSession();
    String userName=userForm.getUserComNum();
    if(userName.equals("admin")){
    officeApplicationForm.setIsassistant(true);
        officeApplicationForm.setIsAdministration(true);
        officeApplicationForm.setIsManager(true);
        officeApplicationForm.setIsFinance(true);
    }else{
    String userid=userForm.getUserId();
        //判断人员角色
        officeApplicationForm.setIsassistant(false);
        officeApplicationForm.setIsAdministration(false);
        officeApplicationForm.setIsManager(false);
        officeApplicationForm.setIsFinance(false);
    List<Position> uplist=this.userRolesBO2.getRolePositionByUserId(userid);
    for(int i=0;i<uplist.size();i++){
    Position up=uplist.get(i);
    String roleName=up.getRoleName();
    Integer roleState=up.getRoleState();
    String adminPositionName=Utils.getOfficeSupplyConfigParam("adminPositionName");
    String assistantPositionName=Utils.getOfficeSupplyConfigParam("assistantPositionName");
    String managerPositionName=Utils.getOfficeSupplyConfigParam("managerPositionName");
    String financePositionName=Utils.getOfficeSupplyConfigParam("financePositionName");
    if(roleName.indexOf(assistantPositionName)!=-1){
    officeApplicationForm.setIsassistant(true);
    }else if(roleName.indexOf(adminPositionName)!=-1){
    officeApplicationForm.setIsAdministration(true);
    }else if(roleName.indexOf(financePositionName)!=-1){
    officeApplicationForm.setIsFinance(true);
    }else if(roleState!=null){
    if(roleState==2){
    officeApplicationForm.setIsManager(true);
    }
    }
    }
    }
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
session.put("officeApplicationForm", officeApplicationForm);
    return "officeSupplyPage";
    }


}

3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="logTest" class="com.dayang.common.util.LogTest" init-method="test"></bean>
<bean id="welcome" class="com.dayang.common.syscommonuse.action.SystemEntryAction"></bean>
<!-- CGLIB代理 -->
<!-- 
<bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="target">
<ref bean="welcome"></ref>
</property>
<property name="interceptorNames">
<list>
<value>logTest</value>
</list>
</property>
</bean>
 -->
<!-- spring自动代理 --> 
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
<bean id="testAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="logTest"></ref>
</property>
<property name="patterns">
<value>.*redirectToOfficeSupplyPage.*</value>
</property>
</bean>  
</beans>

4.测试类

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


import com.dayang.common.basic.InitUserSessionAction;
import com.dayang.common.syscommonuse.action.SystemEntryAction;


public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context=new FileSystemXmlApplicationContext("/WEB-INF/src/logConfig.xml");
SystemEntryAction action=(SystemEntryAction) context.getBean("logProxy");
action.redirectToOfficeSupplyPage();
}
}

 

 

2019-05-05

1.spring1.2.6

<bean id="sessionTest" class="com.dayang.common.util.SessionTest"></bean>
    <bean id="regExpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref bean="sessionTest"></ref>
        </property>
        <property name="patterns">
            <value>.*</value>
        </property>
    </bean>
    <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">        
        <property name="target" ref="officeSupplyAction"></property>
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>regExpAdvisor</value>
            </list>
        </property>
    </bean>

 

2.

package com.dayang.common.util;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.dayang.officeSupply.action.OfficeSupplyAction;

public class SessionTest implements MethodInterceptor{

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
//        HttpServletRequest request = ServletActionContext.getRequest();
//        HttpSession session=request.getSession();
//        UserForm form=(UserForm) session.getAttribute("USER_DETAIL");
        System.out.println("=========="+"AOP TEST before");
        Object result=mi.proceed();
        System.out.println("=========="+"AOP TEST after");
        return result;
    }
    //2019-4-29下午4:35:03,黄娟
    public static void main(String[] args) {
        ApplicationContext actx=new FileSystemXmlApplicationContext("/WEB-INF/src/applicationContext.xml");
        OfficeSupplyAction officeSupplyAction=(OfficeSupplyAction) actx.getBean("logProxy");
        officeSupplyAction.appMainElement();
        
    }
}
 

3.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值