ホームページ  >  記事  >  Java  >  ユーザー操作のインターセプトとロギング - カスタムアノテーション + AOP インターセプト技術の説明

ユーザー操作のインターセプトとロギング - カスタムアノテーション + AOP インターセプト技術の説明

巴扎黑
巴扎黑オリジナル
2017-06-26 11:40:131557ブラウズ

オペレーターとして、システムの運用上の問題に対処するだけでなく、ユーザーから報告された多数のインシデントにも対処する必要があり、この部分の作業には多くの人的資源がかかります。したがって、イベント クエリ処理の一部をセルフサービス プラットフォームにして、ユーザーが自分で確認して処理できるようにすることを検討してください。したがって、ユーザーのセルフサービスシステムがあります。このツール プラットフォームの具体的な実現価値を測定する方法を考慮すると、説得力のあるデータを提供するにはユーザー操作の統計が必要です。

以上がこの記事の背景です。セルフサービス システムのアーキテクチャは、従来の springmvc+spinrg+mybatis+oracle です。ロギングについて考えるときに最初に思い浮かぶのは、AOP インターセプト処理です。関連する技術的な投稿がオンラインに多数あります。単純な小規模プロジェクトで発生する問題は、一般的に Du Niang によって解決できます~(≧▽≦)/~

カスタム アノテーション:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)public @interface LogAnnotation {
    String operateModelName() default "";
    String operateFuncName() default "";
    String operationType() default "";  
    String operateDescribe() default "";
}

コントローラー層のアノテーションを引用

@Controller
@RequestMapping(value="/kit")public class QueryClientInfoController {
    @Resourceprivate IClientService clientService;    private Logger logger = Logger.getLogger(this.getClass().getName());
    
    @RequestMapping(value="/queryClientRegInfo")
    @LogAnnotation(operateModelName="用户注册模块",
    operateFuncName="queryClientRegInfo",
    operationType="query",
    operateDescribe="查询客户注册信息")public void queryClientRegInfo(HttpServletRequest request,HttpServletResponse response){
        String client_idno = request.getParameter("idno");Client client = clientService.queryClientRegInfo(client_idno);//调用service
        System.out.println("queryClientRegInfo:"+client.getName());
        String jsonMap = JSON.toJSONString(client);
        response.setCharacterEncoding("UTF-8");try {
            response.getWriter().write(jsonMap);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }  
    }
}

ユーザー登録情報のクエリに関連する特定の他のサービス層の実装コードは掲載されません。

インターセプターの実装

@Service(value="userLogService")public class UserLogServiceImpl implements IUserLogService {
    @Resourceprivate UserLogDao userLogDao;
    
    @Overridepublic void insert(UserLog record) {return userLogDao.insert(record);
    }@Overridepublic void logBusiController(JoinPoint joinPoint) {
        Map<String, Object> map = new HashMap<String, Object>();
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass;try {
            targetClass = Class.forName(targetName);
            Method[] methods = targetClass.getMethods();for (Method method : methods) {if(method.getName().equals(methodName)) {
                    Class[] clazzs = method.getParameterTypes();if(clazzs.length == arguments.length ) {if(method.getAnnotation(LogAnnotation.class)!= null){
                        map.put("operateModelName", method.getAnnotation(LogAnnotation.class).operateModelName());
                        map.put("operateFuncName", method.getAnnotation(LogAnnotation.class).operateFuncName());
                        map.put("operationType", method.getAnnotation(LogAnnotation.class).operationType());
                        map.put("operateDescribe", method.getAnnotation(LogAnnotation.class).operateDescribe());
                    }elsebreak;
                    }
                }
             }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
        HttpSession session =request.getSession(); 
        User user = (User) session.getAttribute("currentUser");    
        System.out.println("currentUser:"+user.getName()+"\n targetName:"+targetName+"\n methodName:"+methodName+"\n operateModelName:"+map.get("operateModelName")+"\n operateFuncName:"+map.get("operateFuncName")+"\n operationType:"+map.get("operationType")+"\n operateDescribe:"+map.get("operateDescribe"));}
}

次に、構成ファイルでインターセプト設定を行う必要があります:

spring-mvc.xml に

<bean id="LogService" class="com.bbc_kit.common.service.impl.UserLogServiceImpl"></bean>
    <aop:config>
        <aop:pointcut id="logBusiControllerPoint" expression="execution(* com.bbc_kit.business.controller..*.*(..))" />
        <aop:aspect id="logService" ref="LogService">
            <aop:after pointcut-ref="logBusiControllerPoint" method="logBusiController"/>
        </aop:aspect>
    </aop:config>

を追加します 私のコントローラーは、mvc で行われたスキャン インジェクションであることが判明しましたconfiguration 、service は applicationContext.xml で実行されるスキャン インジェクションです。サービス層をインターセプトするすべての操作は、上記の構成の微調整の下で applicationContext.xml に配置できます。

.do

currentUser:renhuang 
targetName:com.bbc_kit.business.controller.QueryClientInfoController 
methodName:queryClientRegInfo 
operateModelName:用户注册模块 
operateFuncName:queryClientRegInfo 
operationType:query 
operateDescribe:查询客户注册信息

を呼び出した後のバックグラウンドログ出力

以上がユーザー操作のインターセプトとロギング - カスタムアノテーション + AOP インターセプト技術の説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。