Spring 注解@Autowired 是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3536674/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:06:49  来源:igfitidea点击:

How does Spring annotation @Autowired work?

springdependency-injectionautowired

提问by Anthony

I came across an example of @Autowired:

我遇到了一个例子@Autowired

public class EmpManager {
   @Autowired
   private EmpDao empDao;
}

I was curious about how the empDaoget sets since there are no setter methods and it is private.

我很好奇empDaoget是如何设置的,因为没有 setter 方法并且它是私有的。

采纳答案by Donal Fellows

Java allows access controls on a field or method to be turned off (yes, there's a security check to pass first) via the AccessibleObject.setAccessible()methodwhich is part of the reflection framework (both Fieldand Methodinherit from AccessibleObject). Once the field can be discovered and written to, it's pretty trivial to do the rest of it; merely a Simple Matter Of Programming.

Java允许上一个字段或方法的访问控制被关断(是的,有一个安全检查到第一通路)经由AccessibleObject.setAccessible()方法,其是反射框架(二者的一部分FieldMethod从继承AccessibleObject)。一旦该字段可以被发现并写入,剩下的工作就变得很简单了。只是一个简单的编程问题

回答by earldouglas

Java allows you to interact with private members of a class via reflection.

Java 允许您通过反射与类的私有成员进行交互。

Check out ReflectionTestUtils, which is very handy for writing unit tests.

查看ReflectionTestUtils,它对于编写单元测试非常方便。

回答by MrJavaJEE

No need for any setter, you just have to declare the EmpDaoclass with the annotation @componentin order that Spring identifies it as part of the components which are contained in the ApplicationContext ...

不需要任何设置器,您只需EmpDao使用注释声明类,@component以便 Spring 将其标识为包含在 ApplicationContext 中的组件的一部分......

You have 2 solutions:

您有 2 个解决方案:

  • To manually declare your beans in the XML file applicationContext :
  • 要在 XML 文件 applicationContext 中手动声明您的 bean:
<bean class="package.EmpDao" />
  • To use automatic detection by seeting these lines in your context file:
  • 要通过在上下文文件中查看这些行来使用自动检测:
<context:component-scan base-package="package" />
<context:annotation-config />

ANDto use the spring annotation to declare the classes that your spring container will manage as components:

AND使用 spring 注释来声明您的 spring 容器将作为组件管理的类:

@Component
class EmpDao {...}

ANDto annotate its reference by @Autowired:

通过以下方式注释其引用@Autowired

@Component (or @Controller, or @Service...)
class myClass {

// tells the application context to inject an instance of EmpDao here
@Autowired
EmpDao empDao;


public void useMyDao()
{
    empDao.method();
}
...
}

Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. Both classes should be beans, i.e. they should be defined to live in the application context.

自动装配是通过将一个 bean 的实例放入另一个 bean 实例中的所需字段来实现的。这两个类都应该是 bean,即它们应该被定义为存在于应用程序上下文中。

Spring knows the existence of the beans EmpDaoand MyClassand will instantiate automatically an instance of EmpDaoin MyClass.

Spring知道豆的存在EmpDao,并MyClass与自动实例的实例EmpDaoMyClass

回答by krock

Spring uses the CGLibAPI to provide autowired dependency injection.

Spring 使用CGLibAPI 来提供自动装配的依赖注入。



References

参考

Further Reading

进一步阅读