何时在Spring中使用自动装配

编程入门 行业动态 更新时间:2024-10-24 04:45:31
本文介绍了何时在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.

看看这里: static.springsource/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,除非用 @Autowired , @Resource , @Inject 。

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

更多推荐

何时在Spring中使用自动装配

本文发布于:2023-11-13 10:34:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!