NutzCN Logo
问答 @Inject 和Mvcs.getIoc.get 方式获得注入的值地址不是同一个
发布于 3158天前 作者 Rekoe 2343 次浏览 复制 上一个帖子 下一个帖子
标签:

code

@IocBean(name = "$aop_autoLoadCache")
public class AutoLoadCacheAopConfigration extends SimpleAopMaker<Cache> {

	@Inject
	private AbstractCacheManager cachePointCut;
}

获得的对象地址与通过

public AbstractCacheManager getCacheManagerByName(HttpServletRequest req, String cacheManagerName) {
		AbstractCacheManager cacheManager = getIoc().get(ShardedCachePointCut.class, "cachePointCut");
		AutoLoadTO queue[] = cacheManager.getAutoLoadHandler().getAutoLoadQueue();
		if (null == queue || queue.length == 0) {
			log.error(cacheManager);
			log.error(cacheManager.getAutoLoadHandler());
			log.error("queue empty");
		}
		return cacheManager;
	}

	private Ioc getIoc() {
		if (this.ioc == null) {
			this.ioc = Mvcs.getIoc();
		}
		return this.ioc;
	}
15 回复

在构造方法内打印堆栈信息,如果真的是两个对象,那么会打印2次或以上

@wendal
现在的表现确实是两个不一样的对象

@wendal
debug 了一下 这对象在加载ioc的时候 被创建了两次 不晓得为啥

@Rekoe 两次的堆栈信息分别贴一下

@wendal
在构造的时候加什么 会打印堆栈信息

@Rekoe

new Throwable().printSxxxxxx();

@wendal

2016-05-01 16:08:11,008 [DEBUG][org.nutz.mvc.impl.Loadings] -    >> add 'com.rekoe.module.OauthLoginAct'
java.lang.Throwable

	at com.jarvis.cache.redis.ShardedCachePointCut.<init>(ShardedCachePointCut.java:47)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:227)
	at org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:62)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:36)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:227)
	at org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:62)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:36)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:227)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:145)
	at org.nutz.plugins.view.freemarker.FreemarkerViewMaker.make(FreemarkerViewMaker.java:18)
	at org.nutz.mvc.view.ViewZone.makeView(ViewZone.java:86)
	at org.nutz.mvc.impl.processor.ViewProcessor.evalView(ViewProcessor.java:88)
	at org.nutz.mvc.impl.processor.ViewProcessor.init(ViewProcessor.java:30)
	at org.nutz.mvc.impl.NutActionChainMaker.eval(NutActionChainMaker.java:38)
	at org.nutz.mvc.impl.UrlMappingImpl.add(UrlMappingImpl.java:47)
	at org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:203)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:107)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)

	at com.rekoe.mvc.RkCmsNutFilter.init(RkCmsNutFilter.java:22)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
	at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)

	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	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:745)
java.lang.Throwable
	at com.jarvis.cache.redis.ShardedCachePointCut.<init>(ShardedCachePointCut.java:47)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:227)
	at org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:62)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:36)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:201)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:227)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:145)
	at org.nutz.plugins.view.freemarker.FreemarkerViewMaker.make(FreemarkerViewMaker.java:18)
	at org.nutz.mvc.view.ViewZone.makeView(ViewZone.java:86)
	at org.nutz.mvc.impl.processor.ViewProcessor.evalView(ViewProcessor.java:88)
	at org.nutz.mvc.impl.processor.ViewProcessor.init(ViewProcessor.java:30)
	at org.nutz.mvc.impl.NutActionChainMaker.eval(NutActionChainMaker.java:38)
	at org.nutz.mvc.impl.UrlMappingImpl.add(UrlMappingImpl.java:47)
	at org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:203)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:107)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at com.rekoe.mvc.RkCmsNutFilter.init(RkCmsNutFilter.java:22)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
	at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	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:745)
2016-05-01 16:08:11,768 [DEBUG][org.nutz.mvc.impl.UrlMappingImpl] -    '/admin/official_server/add' >> AdminOfficialAct.add(...)      : void       | @Ok(fm:template.admin.server.official.add) @Fail(>>:/admin/common/unauthorized.rk) | by 0 Filters | (I:UTF-8/O:UTF-8)

没有定义为单利吧?

@wendal
默认不是单利么

var ioc = {
	jedisPoolConfig : {
		type : "redis.clients.jedis.JedisPoolConfig",
		fields : {
			testWhileIdle : true,
			maxTotal : 100
		}
	},
	shardedJedisPool : {
		type : "redis.clients.jedis.ShardedJedisPool",
		args : [ {
			refer : "jedisPoolConfig"
		}, [ {
			type : "redis.clients.jedis.JedisShardInfo",
			args : [ {
				java : "$conf.get('redis.host', 'localhost')"
			}, 6379, "instance:01" ]
		} ] ],
		events : {
			depose : "destroy"
		}
	},
	autoLoadConfig : {
		type : "com.jarvis.cache.to.AutoLoadConfig",
		fields : {
			threadCnt : 10,
			maxElement : 20000,
			printSlowLog : true,
			slowLoadTime : 500,
			sortType : 1,
			checkFromCacheBeforeLoad : true,
			autoLoadPeriod : 50
		}
	},
	hessianSerializer : {
		type : "com.jarvis.cache.serializer.HessianSerializer"
	},
	cachePointCut : {
		type : "com.jarvis.cache.redis.ShardedCachePointCut",
		args : [ {
			refer : "autoLoadConfig"
		} ],
		fields : {
			serializer : {
				refer : "hessianSerializer"
			},
			shardedJedisPool : {
				refer : "shardedJedisPool"
			},
			namespace : 'test_hessian'
		},
		events : {
			depose : "destroy"
		}
	}
};

@Rekoe 要不debug一下ReferTypeValue看看吧。

正常来说,如果bean生成了,会放入context,并打印日志,第二次的时候就直接从context取了

@wendal
发现个规律
我用两个aop 配置中 都分别引用了

	@Inject
	private AbstractCacheManager cachePointCut;

结果就出现了两次创建

@IocBean(name = "$aop_cache_del")
public class DelCacheAopConfigration extends SimpleAopMaker<CacheDelete> {

	@Inject
	private AbstractCacheManager cachePointCut;
	
	public List<? extends MethodInterceptor> makeIt(CacheDelete cache, Method method, Ioc ioc) {
		return Arrays.asList(new DelCacheAopInterceptor(ioc.get(AbstractCacheManager.class, "cachePointCut"), cache, method));
	}

	public String[] getName() {
		return new String[0];
	}

	public boolean has(String name) {
		return false;
	}
}
@IocBean(name = "$aop_autoLoadCache")
public class AutoLoadCacheAopConfigration extends SimpleAopMaker<Cache> {

	@Inject
	private AbstractCacheManager cachePointCut;

	public List<? extends MethodInterceptor> makeIt(Cache cache, Method method, Ioc ioc) {
		return Arrays.asList(new AutoLoadCacheAopInterceptor(ioc.get(AbstractCacheManager.class, "cachePointCut"), cache, method));
	}

	public String[] getName() {
		return new String[0];
	}

	public boolean has(String name) {
		return false;
	}
}

想不出个原因来

@wendal
很容易重现
直接把Dao注解翻进去 一样能测试出来
前提是你要配置两个 @IocBean(name = "$aop_autoLoadCache") 这样的aop

@IocBean(name = "$aop_autoLoadCache")
public class AutoLoadCacheAopConfigration extends SimpleAopMaker<Cache> {

	@Inject Dao dao;
	public List<? extends MethodInterceptor> makeIt(Cache cache, Method method, Ioc ioc) {
		System.out.println(dao);
		System.out.println(ioc.get(Dao.class));
		return Arrays.asList(new AutoLoadCacheAopInterceptor(ioc.get(AbstractCacheManager.class, "cachePointCut"), cache, method));
	}

	public String[] getName() {
		return new String[0];
	}

	public boolean has(String name) {
		return false;
	}
}

嗯嗯,晚上待我有电脑了试试

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