spring - 基于 Java 与基于注释的配置/ Autowiring

标签 spring

当我使用 Spring 框架时,我经常看到 2 个术语基于 java 和基于注释的配置/ Autowiring 。

基于 Java 的配置/ Autowiring 与基于注释的配置/ Autowiring 不同还是它们是其中之一?

如果它们不同,你能告诉我它们之间有什么不同吗?

最佳答案

基于 Java 的配置:

The official Spring documentation refers to configuring your beans using a Java class annotated with @Configuration and containing @Bean methods as 'Java Configuration'. This allows you to be absolutely free of all XML in your application (at least as far as Spring goes). This support was added in Spring 3.0, and has gotten more powerful.

Source


换句话说,不需要配置文件。如果您的应用程序一切正常。
基于注释的配置:

Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

Source


换句话说,还有 XML 配置文件,但是 bean 连接是使用注解配置的。
注意:注解注入(inject)在 XML 注入(inject)之前执行。因此,对于通过这两种方法连接的属性,后一种配置将覆盖前者。
注意:注解连线在 Spring 容器中默认是不开启的。因此,我们可以使用基于注解的接线,我们需要在 Spring 配置文件中启用它。

关于spring - 基于 Java 与基于注释的配置/ Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615041/

相关文章:

spring - 如何关闭目录的 IntelliJ Spring 配置检查?

java - RESTful 服务的自动类生成器

java - Cookie CsrfTokenRepository.withHttpOnlyFalse() 做什么以及何时使用它?

java - 从 HTML 表单发布时,非英语 UTF 8 字符会发生变化

java - Spring WebExceptionHandler 顺序

java - 二级缓存 + Spring Boot + Java Hibernate 中的问题

java - Spring Data JPA 保存新实体并获取其所有依赖项

java - Mybatis Spring注解异常

java - Spring 启动: create configuration properties bean in runtime

java - Spring 无法 Autowiring Map bean