NutzCN Logo
问答 nutz如何用代码来声明AOP
发布于 2345天前 作者 qq_5a88e4e5 1789 次浏览 复制 上一个帖子 下一个帖子
标签:
var ioc = {
    log : {
        type :'org.nutz.aop.interceptor.LoggingMethodInterceptor'
    },
    myMI : {
        type : 'org.nutz.ioc.aop.config.impl.MyMI'
    },
    pet2 : {
    type : "org.nutz.ioc.aop.config.impl.Pet2"
    },
    
    $aop : {
        type : 'org.nutz.ioc.aop.config.impl.JsonAopConfigration',
        fields : {
            itemList : [
                ['.+','toString','ioc:log'],
                ['.+','.+','ioc:myMI'],
                ['.+','.+','org.nutz.ioc.aop.config.impl.MyMI2','false']
            ]
        }
    }
}

将上面转为代码来配置

9 回复
public class MyJsonAop extends JsonAopConfigration {
    @Override
    public void setItemList(List<List<String>> itemList) {
        itemList = new ArrayList<>();
        itemList.add(Arrays.asList(new String[]{"com.lx.demo.service.*", "^add|update$", "ioc:loginterceptor"}));
        System.out.println(itemList.toString());
        super.setItemList(itemList);
    }
}

这里的方法就没执行过

18-06-15 14:53:52.980 DEBUG [http-nio-8080-exec-5] Load class com.lx.demo.service.Service without AOP

@IocBean(name = "loginterceptor")//声明为一个Ioc的bean,名字为logInterceptor
public class LogInterceptor implements MethodInterceptor {
    public void filter(InterceptorChain chain) throws Throwable {
        System.out.println("方法即将执行 -->" + chain.getCallingMethod());
        chain.doChain();// 继续执行其他拦截器
        System.out.println("方法执行完毕 -->" + chain.getCallingMethod());
    }
}

MyJsonAop加注解

@IocBean(name="$aop_abc")

org.nutz.ioc.IocException: IocBean[nutQuartzCronJobFactory] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:160)
	at com.lx.demo.MainSetupBy.init(MainSetupBy.java:23)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:278)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4637)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5282)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
18-06-15 16:30:25.784 INFO [RMI TCP Connection(3)-127.0.0.1] org.nutz.ioc.impl.NutIoc@1780428000 is closing. startup date [18-06-15 16:30:25.477]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
18-06-15 16:30:25.784 DEBUG [RMI TCP Connection(3)-127.0.0.1] Depose object '$aop_abc' ...
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
18-06-15 16:30:25.785 INFO [RMI TCP Connection(3)-127.0.0.1] org.nutz.ioc.impl.NutIoc@1780428000 is deposed. startup date [18-06-15 16:30:25.477]
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	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:482)
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	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:1468)
	at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
	at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
	at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
	at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
	at sun.rmi.transport.Transport$1.run(Transport.java:200)
	at sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
	at org.nutz.ioc.aop.config.impl.AbstractAopConfigration.getInterceptorPairList(AbstractAopConfigration.java:27)
	at org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:101)
	... 57 more
@IocBean(name="$aop_abc") 加上这个之后

想了想,应该在构造方法里面调用setItems里面的逻辑就好了,

添加回复
请先登陆
回到顶部