NutzCN Logo
问答 @IocBean(create = "init") init()方法没有执行
发布于 3006天前 作者 qq_1968473f 3983 次浏览 复制 上一个帖子 下一个帖子
标签:

在class A上加@IocBean(create = "init“)后, 启动时类A有加载,但是init()方法没有被加载。

启动时日志:
14:17:50.145 DEBUG (AnnotationIocLoader.java:74) addClass - Found @IocBean : class com.×××××.common.×××.A

是因为ioc 懒加载导致的吗?

18 回复

是的. 第一次取的时候才会初始化.

第一次取是指

1)定义如下的形式:

@Inject
A a;

2)还是指具体的调用对象中方法:

a.****();

另外,有没有类似guice eager加载 lazy加载的控制?

@IocBy有个init属性,可以提前加载.

加了init 后,启动时报错Object 'com.cnaisin.common.cache.LocalCache' without define!:

配置如下:================================
@SetupBy(value=MainSetup.class)
@IocBy(type=ComboIocProvider.class,
init={"com.*****.common.***.A"},
args={"*js", "ioc/", "*anno", "com.cnaisin","*tx"
})

报错如下:
16:32:05.251 DEBUG (NutIoc.java:185) get - >> Load definition name=com.cnaisin.common.cache.LocalCache
16:32:05.251 ERROR (NutLoading.java:124) load - Error happend during start serivce!
org.nutz.ioc.IocException: [com.cnaisin.common.cache.LocalCache] # For object [com.cnaisin.common.cache.LocalCache] - type:[]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:226) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.impl.NutLoading.createIoc(NutLoading.java:391) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:102) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.ActionHandler.(ActionHandler.java:19) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.NutFilter._init(NutFilter.java:87) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.NutFilter.init(NutFilter.java:65) ~[nutz-1.r.57.jar:?]
at com.cnaisin.common.mvc.MvcNutzFilter.init(MvcNutzFilter.java:26) ~[classes/:?]
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4561) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204) ~[catalina.jar:8.5.4]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) ~[catalina.jar:8.5.4]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702) ~[catalina.jar:8.5.4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) ~[tomcat-coyote.jar:8.5.4]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_101]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_101]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) ~[catalina.jar:8.5.4]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) ~[catalina.jar:8.5.4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) ~[tomcat-coyote.jar:8.5.4]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_101]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324) ~[?:1.8.0_101]
at sun.rmi.transport.Transport$1.run(Transport.java:200) ~[?:1.8.0_101]
at sun.rmi.transport.Transport$1.run(Transport.java:197) ~[?:1.8.0_101]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_101]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) ~[?:1.8.0_101]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) [?:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_101]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
Caused by: org.nutz.ioc.ObjectLoadException: Object 'com.cnaisin.common.cache.LocalCache' without define!
at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:174) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:188) ~[nutz-1.r.57.jar:?]
... 55 more

那写的不是是ioc bean对应的名字, 应该是 "localCache"

改为"localCache" 也报错了。

报错如下:=================

16:47:50.964 ERROR (NutLoading.java:124) load - Error happend during start serivce!
org.nutz.ioc.IocException: [localCache] # FAIL to create Ioc Bean name=[localCache]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.impl.NutLoading.createIoc(NutLoading.java:391) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:102) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.ActionHandler.(ActionHandler.java:19) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.NutFilter._init(NutFilter.java:87) ~[nutz-1.r.57.jar:?]
at org.nutz.mvc.NutFilter.init(NutFilter.java:65) ~[nutz-1.r.57.jar:?]
at com.cnaisin.common.mvc.MvcNutzFilter.init(MvcNutzFilter.java:26) ~[classes/:?]
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4561) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204) ~[catalina.jar:8.5.4]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) ~[catalina.jar:8.5.4]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) ~[catalina.jar:8.5.4]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702) ~[catalina.jar:8.5.4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) ~[tomcat-coyote.jar:8.5.4]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_101]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_101]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) ~[catalina.jar:8.5.4]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) ~[catalina.jar:8.5.4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) ~[tomcat-coyote.jar:8.5.4]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_101]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) ~[?:1.8.0_101]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_101]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324) ~[?:1.8.0_101]
at sun.rmi.transport.Transport$1.run(Transport.java:200) ~[?:1.8.0_101]
at sun.rmi.transport.Transport$1.run(Transport.java:197) ~[?:1.8.0_101]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_101]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) ~[?:1.8.0_101]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_101]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) [?:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_101]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
Caused by: org.nutz.lang.born.BorningException: Fail to born 'com.cnaisin.common.cache.LocalCache'
by args: []
at org.nutz.lang.Mirror.getBorning(Mirror.java:954) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:107) ~[nutz-1.r.57.jar:?]
... 56 more

这错误, 看上去是LocalCache没有无参数构造方法呢

有无参数构造方法呢!

@IocBean(create = "init")
public class LocalCache extends Cache {
...
private LocalCache() {

}

敢不敢改成public

 private LocalCache() {

}

OK了。改为public 后,就可以了。 实现了eager加载了。

无参数构造方法,一般都是private的呀? 是因为反射实现的导致的吧。

"无参数构造方法,一般都是private的呀" 没听说过...

按照这里的用法来看,无参数构造方法确认应该为public。

但是@IocBy中的init创建@IocBean对象时,对于private类型的无参构造参数要么给个提示,要么直接忽略public/private,直接构造对象。

错误提示不够明显,这个在1.r.58改好了. https://github.com/nutzam/nutz/commit/bc51782f42dcb6c1325f13028f596b7aef29b798

至于"直接忽略public/private,直接构造对象", 我不知道有什么API可以做到,请明示^_^

具体的反射方式我也没深究过。但是从编程语言实现技术来看,无论是反射技术,还是预编译技术都应该可以做到。

只是有没有现成的而己。

@qq_1968473f 简而言之,你只是YY,事实上不可能.

private构造方法可以强行设置为可访问,但不可能"直接忽略".

创建一个对象,总会调用到其构造方法,这是不能避开的.

哈哈。YY只是一种没有经过实践的思考。

我说的"直接忽略public/private,直接构造对象",就是忽略修饰词, 将其设置为可访问。 并不是要忽略其修饰的方法。

对于java底层的反射用得比较少,一般仅限于jdk自身的反射,cglib, javasist的应用。对于dagger, guice,spring的反射实现也没有深入了解过。
和兽总聊反射有些惭愧了。

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