简体   繁体   English

春季自动接线

[英]Autowire in spring

I was trying to check some piece of code within spring framework where the autowired functionality is coded. 我试图在spring框架中检查一些代码,其中已编码自动装配功能。

Let me give you an example. 让我给你举个例子。

@Component
public lass Service{
   @Autowired
   private DaoLayer daoLayer;
}

My question is: How spring can inject the DaoLayer or any instance when the respective field is marked using Autowired. 我的问题是:当使用Autowired标记各个字段时,spring如何注入DaoLayer或任何实例。

  1. Make this using reflection? 使用反射进行此操作? , if reflection is used: Does reflection can access private fields? ,如果使用了反射:反射可以访问私有字段吗?
  2. Make this using a new created proxy? 使用新创建的代理进行此操作? and inject the references on it?, if this happens what happens when getters and setters are not present? 并在上面注入引用?如果发生这种情况,当不存在getter和setter时会发生什么?
  3. Use other technique and which one? 使用其他技术,哪一个?

Could anyone could point me where I can find this information or explain me more about this process. 任何人都可以指出我在哪里可以找到此信息,或者向我解释有关此过程的更多信息。

I remember in sring2.5 getters and setter need to be present but no more in spring3.2, so now spring created them by its own? 我记得在sring2.5中需要存在getter和setter,但在spring3.2中不再存在,所以spring现在是由它们自己创建的吗? or are not used anymore? 还是不再使用?

Thanks. 谢谢。

Make this using reflection? 使用反射进行此操作? , if reflection is used: Does reflection can access private fields? ,如果使用了反射:反射可以访问私有字段吗?

Yes, Spring uses reflection everywhere . 是的,Spring 到处都有反射。 With reflection you can do many things, like access private fields, methods, constructors, and classes. 通过反射,您可以做很多事情,例如访问私有字段,方法,构造函数和类。

Make this using a new created proxy? 使用新创建的代理进行此操作? and inject the references on it?, if this happens what happens when getters and setters are not present? 并在上面注入引用?如果发生这种情况,当不存在getter和setter时会发生什么?

Spring resolves @Autowired on fields directly. Spring直接在字段上解析@Autowired It only needs getters and setters (with Java bean conventional names) to resolve <property> elements of a <bean> declaration while creating the bean. 创建bean时,只需要使用getter和setter(具有Java bean的常规名称)即可解析<bean>声明的<property>元素。 Note that Spring again uses reflection to invoke these getters/setters. 请注意,Spring再次使用反射来调用这些getter / setter。

Use other technique and which one? 使用其他技术,哪一个?

Doesn't need anything else. 不需要其他了

If you're interested in the actual class that does this, look into AutowiredAnnotationBeanPostProcessor . 如果您对执行此操作的实际类感兴趣,请查看AutowiredAnnotationBeanPostProcessor The Spring documentation explains some of this too (look into the IoC chapter). Spring文档也对此进行了解释(请参阅IoC章)。

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

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