我们不鼓励使用Maven + Spring编写XML配置吗?



目前正在学习构建spring应用程序。到目前为止,我已经相当成功地部署了模拟应用程序,但是有一件事一直困扰着我,那就是不理解我们添加到代码中的众多注释背后的机制。看,我不是说我不知道他们的目的,他们在哪里行动,我也不是质疑他们的帮助。

我的观点是,我觉得跳过XML文件中应该做(或正在做?)的更改实际上让我觉得在一天结束时我不知道我真正在写什么。让我说得更具体一些,这样你就可以回答我下面的例子了。这是来自Spring手册。

让我们假设我们有以下配置,将firstMovieCatalog定义为主MovieCatalog

@Configuration
public class MovieConfiguration {
@Bean
@Primary
public MovieCatalog firstMovieCatalog() { ... }
@Bean
public MovieCatalog secondMovieCatalog() { ... }
// ...
}

有了这样的配置,下面的MovieRecommender将自动与firstMovieCatalog .

public class MovieRecommender {
@Autowired
private MovieCatalog movieCatalog;
// ...
}

对应的bean定义如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="example.SimpleMovieCatalog" primary="true">
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<!-- inject any dependencies required by this bean -->
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>

好吧,那么,我想如果你能回答我关于这个例子的两个问题,这将清楚很多我在这里缺乏的理解。

  1. 我不清楚的第一件事是:注释过程是从XML配置中分离出来的过程,还是以某种隐藏的方式生成这个等效的XML配置?

  2. 这个XML配置文件实际上在哪里?我通过Initializr生成的所有spring应用程序都只生成pom.xml文件,它不包括配置。如果我不使用注释,我是否必须在pom中手动编写一个等效的配置?

注释过程是从XML配置中分离出来的一个过程,还是以某种隐藏的方式生成这个等价的XML配置?

Spring在任何情况下都不会生成任何XML或注释。Spring使用XML和注释处理来获取有关哪些组件(类)可以使用以及哪些bean(实例)可以创建、注入和用于处理的信息。然后,可以通过应用程序上下文检索所有这些bean(不要与同名的xml混淆)。

这个XML配置文件实际上在哪里?

Spring的第一个版本使用XML来配置你的应用程序。后来(从Spring 3开始),Spring为添加了注释支持和处理,以简化应用程序配置。注释只是配置组件和bean的另一种方式,而无需维护大型XML文件(超过1000行甚至更多),或者完全避免处理XML。当前的Spring版本支持这两种配置,你也可以混合使用:使用XML和使用注释。

请注意,Spring的ApplicationContext有几个具有不同配置入口点的实现:

  • AnnotationConfigApplicationContext接受@Configuration装饰的类。
  • ClassPathXmlApplicationContext接受应用程序类路径中可用的XML文件路径。

如果我不使用注释,我是否必须在pom中手动编写等效的配置?

首先:POM文件是用于maven处理的,而不是用于Spring。由于您正在使用Maven,并且希望尝试使用不带注释的Spring Boot应用程序,因此您可以使用以下项目结构:

- src
- main
- java
/* your Java packages and files */
- com.example
+ App   <--- Main entry point
- com.example.service
+ Car    <--- Component 1
+ Engine <--- Component 2
- resources
+ my-beans.xml <--- XML configuration. Name can be anything

应用类:

package com.example;
import com.example.service.Car;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
@Configuration
@ImportResource("classpath:my-beans.xml")
public class App {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(App.class, args);
Car car = ctx.getBean(Car.class);
car.move();
}
}

Car.class:

package com.example.service;
public class Car {
private Engine engine;
public void move() {
engine.start();
}
}

Engine.class:

package com.example.service;
public class Engine {
public void start() {
System.out.println("engine starts");
}
}

my-beans.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean class="com.example.service.Engine" />
<bean class="com.example.service.Car" />
<property name="engine" ref="engine"></property>
</bean>
</beans>

注释过程是从XML配置中分离出来的一个过程,还是以某种隐藏的方式生成这个等价的XML配置

当你初始化一个Spring应用程序时,你实例化了一个ApplicationContext:它负责加载所有的上下文定义(bean, services…)。

ApplicationContext实际上是一个接口,它有几个实现取决于你的上下文是如何定义的:

  • ClassPathXmlApplicationContext读取XML文件
  • 基于注释方法的AnnotationConfigApplicationContext

因此,您可以将其视为一个独特的过程,只是数据源不同:XML或注释。但它们描述的是同一件事:上下文。

最新更新