JavaConfig 相对于 Spring 中的 XML 配置的优势?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29162278/
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
Benefits of JavaConfig over XML configurations in Spring?
提问by yathirigan
Earlier the configurations used to be in hard coded in the code, later it was externalized to .property files (for sake of avoiding hard coded values, avoiding changing code for the sake of changing configurations..etc) then it moved to XML (for sake of being more standardized, error free..etc)
之前配置在代码中是硬编码的,后来它被外化到 .property 文件(为了避免硬编码值,避免为了改变配置而改变代码......等)然后它移到 XML(对于为了更加标准化,无错误......等)
Now, while reading about @Configuration in Spring 3 , looks like we are again moving back to the initial approach.
现在,在阅读 Spring 3 中的 @Configuration 时,看起来我们又回到了最初的方法。
Why would we want to hard-code configurations in the code rather than having it externalized ?
为什么我们要在代码中硬编码配置而不是将其外部化?
采纳答案by Narain
There are some advantages
有一些优点
- Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.
- XML based on configuration can quickly grow big. [Yes we can split and import but still]
- Search is much simpler, refactoring will be bliss. Finding a bean definition will be far easier.
- Java 是类型安全的。如果您正在配置正确的 bean 类限定符,编译器将报告问题。
- 基于配置的 XML 可以迅速变大。[是的,我们可以拆分和导入,但仍然可以]
- 搜索要简单得多,重构将是幸福的。查找 bean 定义会容易得多。
There are still people who like XML configuration and continue to do it.
仍然有人喜欢 XML 配置并继续这样做。
References: Java configuration advantagesSome more reasons
回答by majurageerthan
Correct answer was given here, Other-than that answer, I give one more point
According to the Spring 5 reference
XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written. These days many developers choose Java-based configuration for their Spring applications.
基于 XML 的元数据并不是唯一允许的配置元数据形式。Spring IoC 容器本身与实际写入此配置元数据的格式完全分离。如今,许多开发人员为他们的 Spring 应用程序选择基于 Java 的配置。
it means nowadays, people are moving towards java based config
这意味着如今,人们正在转向基于 Java 的配置
for example: Spring web-mvc config in xml
例如: xml 中的 Spring web-mvc 配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="controller" />
<context:component-scan base-package="dao" />
<context:component-scan base-package="service" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
and same config in java based style
和基于 Java 风格的相同配置
@Configuration
@EnableWebMvc
@ComponentScans({
@ComponentScan("controller"),
@ComponentScan("dao"),
@ComponentScan("service")
})
public class WebMVCConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
what is easy to understand? Obviously, the Java-based config is easy to understand.
什么容易理解?显然,基于 Java 的配置很容易理解。
people who write codes and others can easily understand java code than XML. and you do not need to know about XML if you only know Java.
编写代码的人和其他人比 XML 更容易理解 java 代码。如果您只了解 Java,则无需了解 XML。