NutzCN Logo
问答 IocBy init 执行报错
发布于 2326天前 作者 lezhangc 1439 次浏览 复制 上一个帖子 下一个帖子
标签:
====IocBy 
@IocBy(type = ComboIocProvider.class, init={"jedisBean"}, args = {
	"*anno", "club.zhcs", "com.syl", "*tx", "*js", "ioc", "*async", "128", "*quartz", "quartz", "*jedis"
})

====IocBean
@IocBean(create="init")
public class JedisBean {
	
	Log log = Logs.get(); 

	@Inject
	private OrderService orderService;
	
	public void init() {
		
		log.info("jedis======订阅进入了");
		
		RedisInterceptor.jedis().subscribe(new JedisPubSub() {
			
			@Override
			public void onMessage(String channel, String message) {
				if (Strings.equals(channel, Constant.PAY_RESULT_PUB_CHANEL) && Strings.isNotBlank(message)) {
					Message m = Json.fromJson(Message.class, message);
					try {
						orderService.dealPayResult(m.getContent());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}, Constant.PAY_RESULT_PUB_CHANEL);
	}
}

=======错误信息
02-05 18:46:30.656  DEBUG - [LoggingMethodInterceptor.localhost-startStop-1] [afterInvoke] Obj = [com.syl.platform.customer.module.JedisBean$$NUTZAOP] , Return = null , Method = public void com.syl.platform.customer.module.JedisBean.init() , args = []
02-05 18:46:30.656  DEBUG - [ScopeContext.localhost-startStop-1] Remove object 'jedisBean' from [app] 
02-05 18:46:30.661  ERROR - [NutLoading.localhost-startStop-1] Error happend during start serivce!
org.nutz.ioc.IocException: IocBean[jedisBean] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.mvc.impl.NutLoading.createIoc(NutLoading.java:389)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:103)
	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:4590)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5233)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	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 club.zhcs.titans.nutz.interceptor.UserAgentInterceptor.filter(UserAgentInterceptor.java:37)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at org.nutz.aop.interceptor.LoggingMethodInterceptor.filter(LoggingMethodInterceptor.java:48)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at com.syl.platform.customer.module.JedisBean$$NUTZAOP.init(JedisBean.java:1)
	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.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	... 19 more


10 回复
RedisInterceptor.jedis()

这要加@Aop("redis") 才可能生效的,而且报错的不是这个类吧

at club.zhcs.titans.nutz.interceptor.UserAgentInterceptor.filter(UserAgentInterceptor.java:37)

谢谢兽总和蛋蛋的回复,但是titans里面的这个错误确实看不懂了

我就想系统启动的时候启一个redis订阅监听,前面用了@IocBean(create="init") init方法没有执行,看到兽总以前的一些回复,试了下iocby 的init,然后就报这个错了

贴UserAgentInterceptor

看类名,是这个拦截器里面没有考虑到非req作用域的情况

@IocBean(name = "uaDebug")
public class UserAgentInterceptor implements MethodInterceptor {
	Log log = Logs.getLog(UserAgentInterceptor.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nutz.aop.MethodInterceptor#filter(org.nutz.aop.InterceptorChain)
	 */
	@Override
	public void filter(InterceptorChain chain) throws Throwable {
		UserAgent ua = new UserAgent(Mvcs.getReq().getHeader("user-agent"));
		String uaInfo = "ip地址: " + Mvcs.getReq().getRemoteAddr() + " userAgent信息:" + Json.toJson(ua);
		log.debug(uaInfo);
		chain.doChain();
	}

}

Mvcs.getReq() 会返回null,所以npe了,判断一下

兽总好

先测试一下markdown语法 T_T

- 测试测试

这个问题已帮lz解决了,根本原因不在于jedis什么的而是那个npe
UserAgentInterceptor 是aop拦截器的一个实现,而正则表达式给到了一个常用入口包下,正好lz写了一个类是不在module/filter之类的web上下文中
Mvcs.getReq这种万能型懒人api肯定就无法使用了 - -b,换个包明解决之。
另外说一句,其实可以在@SetupBy里面实现这类启动时需要实现的额外逻辑,取ioc容器的依赖随便使用即可

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