Is there a spring lazy proxy factory in Spring?

别来无恙 提交于 2019-12-01 23:42:45

问题


Wicket has this device called a lazy proxy factory. Given:

<property name="foo" ref="beanx"/>

the idea is to auto-generate a proxy in place of 'beanx', and then only initialize beanx if and when something actually calls a method on it.

It seems as if this might be a core Spring capability. Is it there somewhere?


回答1:


See LazyInitTargetSource; that might do what you want. It requires use of lazy-init="true" on the target bean as well, though.




回答2:


Spring singleton beans, the closest thing to what you want, are created when the spring context is initialized: http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes. So I believe the short answer is "no." You can implement your own scope to do this by extending the Spring classes quite easily, though.




回答3:


Spring session/request scope is implemented using the technique you describe, but it is only intended to handle transitions between scope cardinalities, not instance creation. So spring uses the same concepts, but you'd probably have to create your own implementation.



来源:https://stackoverflow.com/questions/2391168/is-there-a-spring-lazy-proxy-factory-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!