什么是 Spring 的“刻板印象”?

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

What is a Spring "stereotype"?

springspring-annotationsstereotype

提问by chad

On a SpringSource blog entry, the following sentence references a stereotype.

在 SpringSource 博客条目中,以下句子引用了一个构造型

Because @Controlleris a specialization of Spring's @ComponentStereotype annotation, the class will automatically be detected by the Spring container as part of the container's component scanning process, creating a bean definition and allowing instances to be dependency injected like any other Spring-managed component.

因为@Controller是 Spring 的@ComponentStereotype 注释的特化,作为容器组件扫描过程的一部分,该类将被 Spring 容器自动检测,创建 bean 定义并允许像任何其他 Spring 管理的组件一样依赖注入实例。

What does this usage of the word stereotype reference? Is this a technical Spring term? Or is stereotype just used in a general sense?

这个词的这个用法是什么意思?这是技术性的 Spring 术语吗?或者刻板印象只是在一般意义上使用?

回答by nicholas.hauschild

The JavaDocsays a bit about it.

JavaDoc的说了一下这件事。

Annotations denoting the roles of types or methods in the overall architecture (at a conceptual, rather than implementation, level).

表示类型或方法在整个架构中的角色的注解(在概念层面,而不是实现层面)。

The noun definition of stereotypefrom Merriam-Webstersays this:

stereotype来自Merriam-Webster的名词定义是这样说的:

something conforming to a fixed or general pattern; especially : a standardized mental picture that is held in common by members of a group and that represents an oversimplified opinion, prejudiced attitude, or uncritical judgment

符合固定或一般模式的东西;尤指:一组成员共同持有的标准化心理图景,代表过于简单化的意见、偏见态度或不加批判的判断

It seems that it is for suggesting a role of particular class that is being annotated. This seems to make sense because it is often recommended that you annotate your Controller classes with @Controller, Service classes with @Service, and so on.

似乎是为了建议正在注释的特定类的角色。这似乎是有道理的,因为通常建议您使用 注释 Controller 类,使用 注释@ControllerService 类@Service,等等。

In addition to the obvious component-scanning functionality, Spring suggests that they make nice point-cut demarcations for your AOP needs.

除了明显的组件扫描功能之外,Spring 还建议它们为您的 AOP 需求做出很好的切入点划分。

回答by aldok

Is this a technical Spring term? Or is stereotype just used in a general sense?

这是技术性的 Spring 术语吗?或者刻板印象只是在一般意义上使用?

I think Spring borrow the term Stereotype from the real world to Spring's technical term.

我认为 Spring 从现实世界借用了术语 Stereotype 到 Spring 的技术术语。

From American English dictionary:

来自美式英语词典:

(noun) a widely held but fixed and oversimplified image or idea of a particular type of person or thing.

(名词)对特定类型的人或事物的广泛持有但固定且过于简单化的形象或想法。

In real world we know some stereotype, for example: American like to drink coffee. British like to drink tea. Of course, its not true for all American or British. Its just an oversimplification of American or British people.

在现实世界中,我们知道一些刻板印象,例如:美国人喜欢喝咖啡。英国人喜欢喝茶。当然,并非所有美国人或英国人都如此。它只是对美国人或英国人的过度简化。

Using stereotype help us making faster decision. When your American friends comes over, instead of asking them "What would you like to drink?" and wait for their response. You can assume that they want a coffee.

使用刻板印象帮助我们做出更快的决定。当你的美国朋友过来时,不要问他们“你想喝什么?” 并等待他们的回应。你可以假设他们想要一杯咖啡。

In Spring, stereotype help us to simplify object creation. You don't need to define the relationship between Type, because you make stereotype of the Type.

在 Spring 中,构造型帮助我们简化了对象的创建。您不需要定义 之间的关系Type,因为您对Type.

Note: Typein Java. Class is a Type.

注意:Type在 Java 中。类是一个Type.



Say we have these classes:

假设我们有这些类:

public abstract class Friend {

    public abstract String favoriteDrink();
}

public class American extends Friend {

    @Override
    public String favoriteDrink() {
        return "Coffee";
    }
}

Without Stereotype

没有刻板印象

You have to define the relationship between Friend and American (Friend is-an American) in a Configuration.

您必须在配置中定义朋友和美国人之间的关系(朋友是美国人)。

@Configuration
public class YourAppConfig {

    @Bean
    public Friend defineFriend() {
        return new American();
    }
}

So, in the test you can verify that:

因此,在测试中您可以验证:

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourAppTest {

    @Autowired
    private Friend friend;

    @Test
    public void drinkTest() {
        assertEquals(friend.favoriteDrink(), "Coffee");
    }
}

With Stereotype

有刻板印象

Stereotype tell Spring that all Friend is an American, directly on the Class declaration.

刻板印象告诉 Spring,所有 Friend 都是美国人,直接在 Class 声明上。

@Component
public class American extends Friend {

    @Override
    public String favoriteDrink() {
        return "Coffee";
    }
}

If your classes encounter a Friend class, it will assume its an American. It's a one-to-one relationship between Friend and American.

如果您的课程遇到 Friend 课程,它将假定它是美国人。这是朋友和美国人之间一对一的关系。

This is very useful if you want your class to behave that way. You don't need to define a Bean to your Configuration file. (You don't even need a Configuration file). Spring will automatically create a Bean from that Stereotype.

如果您希望您的班级以这种方式行事,这将非常有用。您不需要为您的配置文件定义一个 Bean。(您甚至不需要配置文件)。Spring 将自动从该 Stereotype 创建一个 Bean。



That is why the Component, Repository, Service, and Controller annotations belong to Stereotype package. Spring doesn't care much about the detail of your class, from Spring perspective your Classes are either Repository, Service, and Controller, if it doesn't belong to any of that, then its a Component.

这就是 Component、Repository、Service 和 Controller 注释属于 Stereotype 包的原因。Spring 不太关心您的类的细节,从 Spring 的角度来看,您的类要么是Repository, Service,要么是Controller,如果它不属于其中任何一个,那么它是 a Component

Spring just making oversimplificationof your Classes. Hence, the name Stereotype.

Spring 只是过度简化了你的类。因此,名称定型。