简体   繁体   English

为什么我要避免代码中的对象实例化并使用Spring框架?

[英]Why should I want to avoid object instantiation in code and use the Spring framework?

See, when we are explaining Spring Framework to newcomers, we would say we want to avoid new object instantiation in code and Spring will inject dependencies. 看,当我们向新手解释Spring Framework时,我们会说我们希望避免代码中的新对象实例化,Spring会注入依赖关系。 Thereby we will explain IoC and DI. 因此,我们将解释IoC和DI。

My question is: 我的问题是:

"Why do we want Spring instantiation compared to traditional instantiation?" “与传统的实例化相比,我们为什么要进行Spring实例化?”

Is the answer better object management? 答案是更好的对象管理吗? Or simply to take advantage of IoC and DI? 或者只是利用IoC和DI? Please list down concrete answer with explanations. 请列出具体答案和解释。 I am wondering what might be the reason. 我想知道可能是什么原因。

I think spring is better than direct object instantiation because with spring configuration is clearly separated from the other code. 我认为spring比直接对象实例化更好,因为spring配置明显与其他代码分开。 This makes the components loosely coupled. 这使得组件松散耦合。 Due to loose coupling it is very easy to replace/modify one component without affecting others. 由于松耦合,很容易更换/修改一个组件而不影响其他组件。 This makes the code maintainable and more importantly testable leading to better quality software. 这使代码可维护 ,更重要的是可测试,从而产生更高质量的软件。

Because this make you focus on developing components, and frees you from developing the boiler plate code to set these components up in ONE way. 因为这使您专注于开发组件,并使您免于开发锅炉板代码以单向设置这些组件。

By using Spring, everyone can take your application, and replace components with own implementations, like an own authentication provider, without interfering with the rest of the application. 通过使用Spring,每个人都可以使用您的应用程序,并使用自己的实现替换组件,例如自己的身份验证提供程序,而不会干扰应用程序的其余部分。

In addition it frees you from writing explicit configuration file handling, because the Spring configuration IS the application configuration. 此外,它还使您无需编写显式配置文件处理,因为Spring配置是应用程序配置。

Spring is very nice for large apps to be deployed and used by many. Spring非常适合许多人部署和使用大型应用程序。

Use it sparingly. 谨慎使用它。 IOC is great for test-driven development, as it is easy to replace a component with a 'test' component. IOC非常适合测试驱动开发,因为很容易用'test'组件替换组件。

It is important to understand exactly what component will be in your application. 确切了解应用程序中的组件非常重要。 IOC is not meant to replace all uses of instantiation. IOC并不是要取代实例化的所有用途。

IMHO, Springs dependency injection supports and encourages use of good programming practices, like using interfaces (by-type injection) and modularizing things to different layers instead of writing huge classes (if you've ever seen "good old everything in Servlet"-webapps, you probably know what I mean). 恕我直言,Springs依赖注入支持并鼓励使用良好的编程实践,例如使用接口(按类型注入)和将事物模块化到不同的层而不是编写大型类(如果你曾见过“旧的一切都在Servlet中”-webapps ,你可能知道我的意思)。 Since injecting a service- or DAO-object can be done easily, like with just a simple annotation (like @Autowired) or via XML-configuration, modularizing applications is fast and easy. 由于只需简单的注释(如@Autowired)或通过XML配置即可轻松完成注入服务或DAO对象,因此模块化应用程序既快速又简单。

I'd stay it's still not useful to handle instantiation of everything via Spring, you can still create objects with new , if the objects are such that Spring doesn't need to be aware of them or you just use them somewhere for moving data between layers etc. 我仍然没有办法通过Spring处理所有内容的实例化,你仍然可以使用new创建对象,如果对象是Spring不需要知道它们或者你只是在某处使用它们来移动数据层等

Some people have complained about the xml-configuration with Spring. 有些人抱怨Spring的xml配置。 With Spring 3.0, the new annotations make coding and simple configurations very fast, and your XML-configuration file needs only to contain the actual configuration of the software, not all the injections and "configuration" that actually never changes.. as an example, a few years back, we built a relatively simple in-house application with Spring 2.5 (it did have some annotations already, but we didn't use them), and the size of xml-configuration files grew to over 1000 lines in total very fast. 使用Spring 3.0,新注释可以非常快速地进行编码和简单配置,并且您的XML配置文件只需要包含软件的实际配置,而不是所有实际上永远不会更改的注入和“配置”。例如,几年前,我们使用Spring 2.5构建了一个相对简单的内部应用程序(它确实有一些注释,但我们没有使用它们),并且xml配置文件的大小总共超过1000行快速。 Currently, we have a project with Spring 3.0 and the XML file contains only 100 lines, the stuff that actually needs to be configurable after building the project. 目前,我们有一个Spring 3.0项目,XML文件只包含100行,这些内容在构建项目后实际上需要配置。

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

相关问题 为什么我应该在Java应用程序上使用Spring核心框架 - Why should I use Spring core framework over java application JBoss,Spring:persistence.xml-为什么我应该避免使用该文件名? - JBoss, Spring: persistence.xml - why should i avoid this filename? 类的实例化-Spring框架 - Instantiation of class - Spring framework Java如果我具有不同的构造函数但具有相同的接口,则应为对象实例化使用哪种设计模式? - Java what design pattern should I use for object instantiation if I have different constructor but same interface? 我为什么要使用Spring Android? - Why should I use Spring Android? 我应该使用哪种设计模式? 使用Spring框架 - What design pattern should I use ? using spring framework 我应该使用框架吗? - Should I use a framework? 为什么要避免Java中的大整数实例化 - Why to avoid biginteger instantiation in Java 如果我不想将我的对象用作哈希表中的键,为什么两个相等的对象应返回相等的哈希码? - Why should two equal objects return equal hash codes if I don't want to use my object as a key in a hash table? 我是否应该使用OVal(对象验证框架),即使它不需要AspectJ? - Should I use OVal (Object Validation Framework) even if it requires AspectJ or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM