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

查看:113
本文介绍了自动装配如何在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

在我的控制器中,我将如何实例化此服务的实例

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

我是否会执行以下操作?

Would I just do the following?

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 实例化对象,而不是你。即 - 你永远不会使新的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你的控制器,你只需要有以下内容:

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 中,您应启用<上下文:component-scan> 以便扫描类 @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天全站免登陆