简体   繁体   English

Spring Framework和其他IoC容器的内部结构

[英]Internals of Spring Framework and other IoC containers

I've been using spring for some time, but I always wondered how does it work, more specifically, how do they load and weave beans/classes marked only with an interface or @annotation . 我一直在使用spring一段时间,但我总是想知道它是如何工作的,更具体地说,它们如何加载和编织只用接口或@annotation标记的bean /类。

For the xml declarations, it's easy to see how spring preprocesses my beans (they are declared in the xml context that spring reads), but for the classes marked only with annotations, I can't see how that works, since I don't pass any agent to the jvm or so. 对于xml声明,很容易看到spring如何预处理我的bean(它们是在spring读取的xml上下文中声明的),但是对于仅标注注释的类,我看不出它是如何工作的,因为我没有将任何代理传递给jvm左右。

I believe there is some Java/JVM hook that allows you to preprocess classes by some sort of criteria, but I wasn't able to found out anything on the docs. 我相信有一些Java / JVM钩子允许你通过某种标准预处理类,但我无法在文档上找到任何东西。

Can someone point me to some docs? 有人能指点我一些文件吗? Is this related to the java.lang.instrument.ClassFileTransformer API? 这与java.lang.instrument.ClassFileTransformer API有关吗?

Actually by default Spring does not do any bytecode postprocessing neither for XML-, nor annotation-configured beans. 实际上,默认情况下,Spring既不对XML-也不对注释配置的bean进行任何字节码后处理。 Instead relevant beans are wrapped into dynamic proxies (see eg java.lang.reflect.Proxy in the Java SDK). 相反,相关的bean被包装到动态代理中(参见Java SDK中的java.lang.reflect.Proxy )。 Dynamic proxies wrap the actual objects you use and intercept method calls, allowing to apply AOP advices. 动态代理包装您使用的实际对象并拦截方法调用,允许应用AOP建议。 The difference is that proxies are essentially new artificial classes created by the framework, whereas weaving/bytecode postprocessing changes the existing ones. 不同之处在于代理本质上是由框架创建的新的人工类,而编织/字节码后处理会改变现有的类。 The latter is impossible without using the Instrumentation API you mentioned. 如果不使用您提到的Instrumentation API,后者是不可能的。

As for the annotations, the implementation of <context:component-scan> tag will scan the classpath for all classes with the Spring annotations and create Spring metadata placeholders for them. 至于注释, <context:component-scan>标签的实现将使用Spring注释扫描所有类的类路径,并为它们创建Spring元数据占位符。 After that they are treated as if they were configured via XML (or to be more specific both are treated the same). 之后,它们被视为通过XML配置(或者更具体地说两者都被视为相同)。

Although Spring doesn't do bytecode postprocessing itself you can configure the AspectJ weaving agent that should work just fine with Spring, if proxies do not satisfy you. 虽然Spring本身不进行字节码后处理,但是如果代理不满足你的话,你可以配置应该与Spring一起工作的AspectJ编织代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM