简体   繁体   English

为什么我们在 Spring 应用程序中需要 getters/setters?

[英]Why do we have a need of getters/setters in Spring application?

Sometimes I encounter with the getters/setters in Spring code.有时我会遇到 Spring 代码中的 getters/setters。

Is there any alternate to this technique?这种技术有什么替代方法吗?

I'm new to this technique.我是这项技术的新手。

I assume you know the motivation for getters and setters in general;我假设您大致了解 getter 和 setter 的动机; if not, see Why use getters and setters/accessors?如果不是,请参阅为什么使用 getter 和 setter/访问器?

As for Spring, accessors aren't strictly needed in most cases (but considering the huge amount of libraries in Spring, in some cases you might need them).至于 Spring,在大多数情况下并不严格需要访问器(但考虑到 Spring 中的大量库,在某些情况下您可能需要它们)。 They're often believed to be needed for Spring to inject dependencies, but in most cases Spring can inject into constructors or public fields.通常认为 Spring 需要它们来注入依赖项,但在大多数情况下 Spring 可以注入构造函数或公共字段。

There are valid reasons for using accessors instead of public fields for mutable objects, but that's a matter of taste and style.对于可变对象使用访问器而不是公共字段是有正当理由的,但这是品味和风格的问题。 IDEs like IntelliJ can automatically add accessors for unencapsulated fields (or de-encapsulate private fields with accessors) so unless you're writing a 3rd party library others will use as a dependency, it's not really that important what you choose.像 IntelliJ 这样的 IDE 可以自动为未封装的字段添加访问器(或使用访问器取消封装私有字段),所以除非你正在编写一个第三方库,否则其他人将用作依赖项,你选择什么并不是那么重要。

Spring doesn't need getters and setters. Spring 不需要 getter 和 setter。

Most injection should be done through constructors.大多数注入应该通过构造函数完成。 Only optional dependencies should be injected through setters.只有可选的依赖项应该通过 setter 注入。 If you have a problem with setters you may always inject into fields directly, although I would prefer the setters, because you can use those to setup beans in tests without Spring and without reflection.如果你对 setter 有问题,你可能总是直接注入字段,尽管我更喜欢 setter,因为你可以在测试中使用它们来设置 bean,而无需 Spring 和反射。

I don't know of a place in Spring where getters are required.我不知道 Spring 中需要吸气剂的地方。

Of course, Spring is all about integrating pieces and many of those pieces might require getters and or setters, since it is a common pattern in Java (although a rather stupid one).当然,Spring 都是关于集成部分的,其中许多部分可能需要 getter 和/或 setter,因为它是 Java 中的常见模式(尽管是一个相当愚蠢的模式)。 For these cases one would have to look at the individual libraries.对于这些情况,必须查看各个库。

Two reasons i know:我知道的两个原因:

1. we have a entity like this: 1.我们有这样一个实体:

public class Person {
    public int age = 50;
}

then we use that for 100+ other service like this:然后我们将它用于 100 多个其他服务,如下所示:

public class S1{
    displayAge(Person p) {
        System.out.println(p.age)
    }
}
public class S2{
    displayAge(Person p) {
        System.out.println(p.age)
    }
}
.
.
.
public class SN{
    displayAge(Person p) {
        System.out.println(p.age)
    }
}

Now the requirements have changed, if the age is greater than 50 years old, it should display "old people", what would you do?现在要求变了,如果年龄大于50岁,要显示“老人”,怎么办? you must change all service.您必须更改所有服务。 but if use getter, would be like this:但如果使用 getter,将是这样的:

public class Person {
    public int age = 50;
    public String getAge() {
        return age > 50 ? "old people" : "young people";
    }
}

public class S1{
    displayAge(Person p) {
        System.out.println(p.getAge())
    }
}

public class S2{
    displayAge(Person p) {
        System.out.println(p.getAge())
    }
}
.
.
.
public class SN{
    displayAge(Person p) {
        System.out.println(p.getAge())
    }
}

You don't need to hack to change all services, just change the getter or setter.您无需修改所有服务,只需更改 getter 或 setter。 Of course, this is a simple example, in practice, you need to decide for yourself.当然,这是一个简单的例子,在实际操作中,需要你自己决定。

2. Many frameworks, when using, will call the getter or setter method of the corresponding property through reflection according to the property of the class. If you use this kind of framework, you need to provide the corresponding getter and setter method, such as some frameworks for operating databases. 2.很多框架,在使用的时候,会根据class的属性,通过反射调用相应属性的getter或者setter方法,如果使用这种框架,需要提供相应的getter和setter方法,比如一些操作数据库的框架。

At last,If you just use an object to transfer data yourself, you can actually do without getters and setters at all.最后,如果你自己用一个object来传输数据,其实完全可以不用getters和setters。

In fact, the rules and regulations during development are for better collaboration with others, or to make the program more robust.实际上,开发时的规章制度是为了更好地与他人协作,或者是为了让程序更加健壮。 If you can control it well, you don't have to be dogmatic.如果你能很好地控制它,你就不必教条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM