自动装配在 Spring 中是如何工作的? [英] How does autowiring work in Spring?

查看:22
本文介绍了自动装配在 Spring 中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 控制反转 (IoC) 在 Spring 中工作.

I'm a little confused as to how the inversion of control (IoC) works in Spring.

假设我有一个名为 UserServiceImpl 的服务类,它实现了 UserService 接口.

Say I have a service class called UserServiceImpl that implements UserService interface.

这将如何@Autowired?

在我的Controllers中,我将如何实例化这个服务的instance?

And in my Controllers, how would I instantiate an instance of this service?

我会做以下事情吗?

UserService userService = new UserServiceImpl();

推荐答案

首先,也是最重要的 - 所有 Spring bean 都是被管理的 - 它们存在于"一个容器中,称为应用程序上下文".

First, and most important - all Spring beans are managed - they "live" inside a container, called "application context".

其次,每个应用程序都有一个该上下文的入口点.Web 应用程序有一个 Servlet,JSF 使用一个 el-resolver,等等.另外,还有一个地方应用程序上下文被引导和所有 bean - 自动装配.在 Web 应用程序中,这可以是启动侦听器.

Second, each application has an entry point to that context. Web applications have a Servlet, JSF uses a el-resolver, etc. Also, there is a place where the application context is bootstrapped and all beans - autowired. In web applications this can be a startup listener.

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

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.

在应用程序上下文中什么是生活"?这意味着 context 实例化对象,而不是你.IE.- 你永远不会创建 new UserServiceImpl() - 容器会找到每个注入点并在那里设置一个实例.

What is "living" in the application context? This means that the context instantiates the objects, not you. I.e. - you never make new UserServiceImpl() - the container finds each injection point and sets an instance there.

在您的控制器中,您只有以下内容:

In your controllers, you just have the following:

@Controller // Defines that this class is a spring bean
@RequestMapping("/users")
public class SomeController {

    // Tells the application context to inject an instance of UserService here
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public void login(@RequestParam("username") String username,
           @RequestParam("password") String password) {

        // The UserServiceImpl is already injected and you can use it
        userService.login(username, password);

    }
}

一些注意事项:

  • 在您的 applicationContext.xml 中,您应该启用 以便为 @Controller 扫描类、@Service 等注解.
  • Spring-MVC 应用程序的入口点是 DispatcherServlet,但它对您是隐藏的,因此应用程序上下文的直接交互和引导发生在幕后.
  • UserServiceImpl 也应该定义为 bean - 使用 <bean id=".." class=".."> 或使用 @Service 注释.由于它将是 UserService 的唯一实现者,因此将被注入.
  • 除了 @Autowired 注释之外,Spring 还可以使用 XML 可配置的自动装配.在这种情况下,所有名称或类型与现有 bean 匹配的字段都会自动注入 bean.事实上,这是自动装配的最初想法——在没有任何配置的情况下注入依赖项的字段.也可以使用其他注释,如 @Inject@Resource.
  • In your applicationContext.xml you should enable the <context:component-scan> so that classes are scanned for the @Controller, @Service, etc. annotations.
  • The entry point for a Spring-MVC application is the DispatcherServlet, but it is hidden from you, and hence the direct interaction and bootstrapping of the application context happens behind the scene.
  • UserServiceImpl should also be defined as bean - either using <bean id=".." class=".."> or using the @Service annotation. Since it will be the only implementor of UserService, it will be injected.
  • Apart from the @Autowired annotation, Spring can use XML-configurable autowiring. In that case all fields that have a name or type that matches with an existing bean automatically get a bean injected. In fact, that was the initial idea of autowiring - to have fields injected with dependencies without any configuration. Other annotations like @Inject, @Resource can also be used.

这篇关于自动装配在 Spring 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆