1. ホーム

一致するワイルドカードは厳密ですが、要素 'aop:aspectj' の宣言が見つかりません。

2022-02-12 01:23:33

システムメソッドに費やされる時間をカウントするAOPタンジェントを追加する。

package com.xyzq.afa.pbank.console.aop;

/**
 * Created by YANG on 2017-7-6.
 */
import com.xyzq.afa.util.PBankUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang;
import org.aspectj.lang.annotation;
import org.aspectj.lang.annotation;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation;
import org.aspectj.lang.reflect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype;

import java.util.Calendar;
import java.util.Collection;
import java.util.Map;
import java.util;

@Component
@Aspect
public class TimeConsumeLogAspect {
    private ThreadLocal<Long> time = new ThreadLocal<Long>();
    private ThreadLocal<String> tag = new ThreadLocal<String>();

// @Autowired
// private PermissionService permissionService;

    @Around("execute(* com.xyzq.afa.pbank.console.*.service*. *. *Impl.*(...)) ")
    public Object aroundService(ProceedingJoinPoint joinPoint) throws Throwable {
        Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass().getName());

        time.set(System.currentTimeMillis());
        tag.set(UUID.randomUUID().toString());

// String username = (String) SecurityUtils.getSubject().getPrincipal();


        // Spell the parameters into a string
        StringBuffer msg = new StringBuffer();
        // retrieve the methods parameter types (static):
        final Signature signature = joinPoint.getStaticPart().getSignature();
        if (signature instanceof MethodSignature) {
            final MethodSignature ms = (MethodSignature) signature;
            final Class<? >[] parameterTypes = ms.getParameterTypes();
            for (final Class<? > pt : parameterTypes) {
                msg.append("Parameter type:").append(pt);
            }
        }

        // retrieve the runtime method arguments (dynamic)
        if (logger.isDebugEnabled()) {
            for (final Object argument : joinPoint.getArgs()) {
                msg.append(" Parameter value:");
                msg.append(PBankUtil.getObjectAllAttributes(argument));
            }
        } else {
            for (final Object argument : joinPoint.getArgs()) {
                msg.append(" Parameter value:");
                msg.append(argument);
            }
        }

        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        // Logging before Service execution
        logger.info("[TagId: {} ]Service Starting. method : {} ({})", tag.get(), ms.getMethod().getName(),
                msg.toString());

        //execute the target method and get its return value
        Object rtn = joinPoint.proceed(joinPoint.getArgs());

        int size = -1;
        if (rtn instanceof Collection) {
            size = ((Collection) rtn).size();
        } else if (rtn instanceof Map) {
            size = ((Map) rtn).size();
        }
        //Log after Service execution
        if (logger.isDebugEnabled()) {
            logger.info("[TagId: {} ]Service Finished. used time {}ms. return: {}", tag.get(),
                    System.currentTimeMillis() - time.get(), rtn);
        } else {
            if (size > -1) {
                // return value of collection type, only print collection size in non-debug mode
                logger.info("[TagId: {} ] Service Finished. used time {}ms. return: {}", tag.get(),
                        System.currentTimeMillis() - time.get(), size);
            } else {
                // If the return value is not a collection type, it is printed directly
                logger.info("[TagId: {} ]Service Finished. used time {}ms. return: {}", tag.get(),
                        System.currentTimeMillis() - time.get(), rtn);
            }
        }

        return rtn;
    }

    @Before("execute(* com.xyzq.afa.pbank.console.web.controllers.*. *Controller.*(...)) ")
    public void beforeController(JoinPoint joinPoint) {
        Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass().getName());

        time.set(System.currentTimeMillis());
        tag.set(UUID.randomUUID().toString());
        //spell the parameters into a string
        StringBuffer msg = new StringBuffer();
        // retrieve the methods parameter types (static):
        final Signature signature = joinPoint.getStaticPart().getSignature();
        if (signature instanceof MethodSignature) {
            final MethodSignature ms = (MethodSignature) signature;
            final Class<? >[] parameterTypes = ms.getParameterTypes();
            for (final Class<? > pt : parameterTypes) {
                msg.append("Parameter type:").append(pt);
            }
        }

        // retrieve the runtime method arguments (dynamic)
        if (logger.isDebugEnabled()) {
            for (final Object argument : joinPoint.getArgs()) {
                msg.append(" Parameter value:");
                msg.append(PBankUtil.getObjectAllAttributes(argument));
            }
        } else {
            for (final Object argument : joinPoint.getArgs()) {
                msg.append(" Parameter value:");
                msg.append(argument);
            }
        }
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
// // Confirm user login information
// User loginUser = permissionService.getLoginUser();
// logger.debug("[TagId: {} UserName {} ]Controller Starting. method : {} ({})", tag.get(), (String) SecurityUtils.getSubject(). getPrincipal(),
// ms.getMethod().getName(), msg.toString());
        logger.info("[TagId: {} ]Controller Starting. Method : {} ({})", tag.get(),
                ms.getMethod().getName(), msg.toString());
    }

    @After("execute(* com.xyzq.afa.pbank.console.web.controllers.*. *Controller.*(...)) ")
    public void afterController(JoinPoint joinPoint) {
        Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass().getName());

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        MethodSignature ms = (MethodSigna

XML ファイル applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       default-lazy-init="true">

    <context:annotation-config />
    <context:component-scan base-package="com.xyzq" />
    <aop:aspectj-autoproxy proxy-target-class="false" />
    <bean name="bartPlaceholderConfigurer" class="com.xyzq.simpson.bart.client.spring.BartPlaceholderConfigurer"& gt;
        <property name="location" value="classpath:config/app.properties" />
    </bean>
</beans>

プロジェクト起動後に例外を投げるのは以下の通りです。

:Line 12 in XML document from file [D:\IdeaWorks\PrivateBank_test\pbank-console\pbank-console-web\target\pbank-console-web-1.0.0\WEB-INF\ classes\config\spring\applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 29; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from file [D:\IdeaWorks\PrivateBank_test\ pbank-console\pbank-console-web\target\pbank-console-web-1.0.0\WEB-INF\classes\config\spring\applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 29; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174 )
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209 )
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180 )
	at net.paoding.rose.load.context.RoseWebAppContext.loadBeanDefinitions(RoseWebAppContext.java:90)
	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory( AbstractRefreshableApplicationContext.java:130)
	at org.springframework.context.support.AbstractApplicationContext.achieveFreshBeanFactory(AbstractApplicationContext.java:537)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
	at net.paoding.rose.RoseFilter.prepareRootApplicationContext(RoseFilter.j
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1863)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
	at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
	at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
	at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
	at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
	at sun.rmi.transport.Transport$2.run(Transport.java:202)
	at sun.rmi.transport.Transport$2.run(Transport.java:199)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:198)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 29; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.
	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
	at org.apache.x

これは、schemaLocation に AOP 関連の XSD 設定がないためで、XML ファイルに新しい AOP 設定を追加すれば解決します。

修正内容は次のようになります。

<?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:context="http://www.springframework.org/schema/context"。
       xmlns:aop="http://www.springframework.org/schema/aop"。
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"。

       default-lazy-init="true">
    <context:annotation-config />
    <context:component-scan base-package="com.xyzq" />
    <aop:aspectj-autoproxy proxy-target-class="false" />
    <bean name="bartPlaceholderConfigurer" class="com.xyzq.simpson.bart.client.spring.BartPlaceholderConfigurer"&gt;
        <property name="location" value="classpath:config/app.properties" />
    </bean>
</beans>

再度、プログラムを起動すると正常に起動します。