java Wicket 的 @SpringBean 注释是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1947389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Wicket's @SpringBean annotation work?
提问by Tarquila
How does Wicket's @SpringBeanannotation work? Does it use reflection at run time? Does it make the compiler inject some code? Or what?
Wicket 的@SpringBean注解是如何工作的?它在运行时使用反射吗?它会让编译器注入一些代码吗?要不然是啥?
采纳答案by Thomas Jung
Spring uses the class loader and ASM at runtime to find all annotated classes.
Spring 在运行时使用类加载器和 ASM 来查找所有带注释的类。
You can configurewhere spring should search for beans:
您可以配置spring 应该在哪里搜索 bean:
<context:component-scan base-package="some.package.to.start.from"/>
This uses the ClassPathBeanDefinitionScannerinternally which will use the PathMatchingResourcePatternResolverto find the classes and the ASM-based MetadataReaderto read the annotations.
这在内部使用ClassPathBeanDefinitionScanner,它将使用PathMatchingResourcePatternResolver来查找类和基于 ASM 的MetadataReader来读取注释。
回答by skaffman
@SpringBeanworks using Wicket's underlying Injector mechanism. When you instantiate a Wicket component, the constructor of Wicket's component base class introspects the class being instantiated, looking for the @SpringBeanannotation. If the bean is found, then Wicket generates a proxy for the spring bean and injects it into the component's field. This is Wicket's equivalent of Spring's @Autowiredannotation, the effect is similar.
@SpringBean使用 Wicket 的底层注入器机制工作。当您实例化 Wicket 组件时,Wicket 组件基类的构造函数会内省正在实例化的类,寻找@SpringBean注释。如果找到了 bean,则 Wicket 为 spring bean 生成一个代理并将其注入到组件的字段中。这是Wicket相当于Spring的@Autowired注解,效果类似。
It doesn't, however, have anything to do with Spring's own context/classpath scanning functionality (e.g. @Component), which is about auto-discovery of what is and isn't a bean, rather having anything to do with wiring.
然而,它与 Spring 自己的上下文/类路径扫描功能(例如@Component)无关,它是关于自动发现什么是和不是 bean,而是与接线有关。
回答by Rod
The class marked with a @SpringBean annotation has to have one of:
用 @SpringBean 注释标记的类必须具有以下之一:
- A no-args constructor
- A superclass with a no-args constructor
- Implement an interface
- 无参数构造函数
- 具有无参数构造函数的超类
- 实现一个接口
An exception will be thrown if these conditions are not met as Wicket will not be able to proxy the class.
如果不满足这些条件,则会引发异常,因为 Wicket 将无法代理该类。

