何时在Spring中使用自动装配 [英] When to use autowiring in Spring

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

问题描述

我正在阅读 Pro Spring 3 这本书。它有一段让我很困惑的段落。这一段是关于春天的自动装配。以下是摘录:

I am reading the book Pro Spring 3. It has a certain paragraph that really confused me. The paragraph is about autowiring in spring. Here is an excerpt:


在大多数情况下,你是否应该使用
自动装配问题的答案肯定是不 自动装配可以节省您在小型
应用程序中的时间,但在许多情况下,它会导致不良做法,并且在大型应用程序中缺乏灵活性。使用byName似乎是一个很好的
想法,但它可能会导致你给你的类人工属性
名称,这样你就可以利用自动装配功能。
Spring背后的整个想法是,你可以创建你喜欢的
类,让Spring为你工作,而不是相反...

In most cases, the answer to the question of whether you should use autowiring is definitely "no!" Autowiring can save you time in small applications, but in many cases, it leads to bad practices and is inflexible in large applications. Using byName seems like a good idea, but it may lead you to give your classes artificial property names so that you can take advantage of the autowiring functionality. The whole idea behind Spring is that you can create your classes how you like and have Spring work for you, not the other way around ...

...对于任何非常重要的应用程序,避免以所有
成本自动装配。

... For any nontrivial application, steer clear of autowiring at all costs.

我一直都是在我创建的应用程序中使用@Autowired标记。有人可以解释它的错误以及我应该使用什么吗?

I have always been using the @Autowired tag in applications I have created. Can someone explain what is wrong with it and what I should use instead?

我现在处理大多数事情的一个小例子是:

A mini example on how I handle most things now is:

@Service("snippetService")
public class SnippetService {

    @Autowired
    private TestService testService;

    public Snippet getSnippet() {
        return testService.getSnippet();
    }
}    

使用自动装配这样错误还是我遗失了什么?

Is using autowiring like this "wrong" or am I missing something?

推荐答案

我相信这里有两件事情混乱。本章中自动装配的含义是标记bean,用于自动检测和注入依赖关系。这可以通过设置autowirebean属性来实现。

I believe there are two things confused here. What is meant by 'autowiring' in this chapter is marking bean for automated detection and injection of dependencies. This can be achieved through setting of "autowire" bean attribute.

这实际上与使用 @Autowired 相反你明确指出依赖注入的字段或setter。

This is in fact opposed to using @Autowired where you explicitely indicate field or setter for dependency injection.

看看这里: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans .html#beans-factory-autowire

为了解释它,假设你有

public class SnippetService {

    private TestService testService;

    public Snippet getSnippet() {
        return testService.getSnippet();
    }

    public void setTestService(TestService testService) {
      this.testService = testService;
    }
}

如果你定义了一个bean:

If you defined a bean:

<bean class="mypackage.SnippetService" autowire="byType"/>

spring会尝试注入匹配类型的bean, TestService 在这种情况下,通过调用setTestService setter。即使您没有使用 @Autowired 。这确实很危险,因为一些人可能不会被春天召唤。

spring would attempt to inject bean of matching type, TestService in this case, by calling setTestService setter. Even though you did not use @Autowired. This indeed is dangerous since some setters might not be meant to be called by spring.

如果你设置autowire =no,除非用<标记,否则不会注入任何东西code> @Autowired , @Resource @Inject

If you set autowire="no", nothing will be injected unless marked so with @Autowired, @Resource, @Inject.

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

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