当前位置: 首页 > 知识库问答 >
问题:

为什么Spring Boot加载所有xml文件,而我只指定了一个?

嵇永望
2023-03-14

我有一个非常基本的Spring Boot应用程序。

项目中有2个@SpringBootApplication%s:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

/**
 * Created on 2/16/16.
 */
@SpringBootApplication
@ImportResource("classpath:run-app-a-context.xml")
public class ApplicationA {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ApplicationA.class, args);

        System.out.println(context.getBean("helloA"));
        System.out.println(context.getBean("helloB"));
    }
}
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

/**
 * Created on 2/16/16.
 */
@SpringBootApplication
@ImportResource("classpath:run-app-b-context.xml")
public class ApplicationB {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ApplicationB.class, args);

        System.out.println(context.getBean("helloA"));
        System.out.println(context.getBean("helloB"));
    }
}
./gradlew bootRun
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.1.RELEASE)

2016-02-16 11:42:21.889  INFO 47524 --- [           main] com.example.ApplicationA                 : Starting ApplicationA on LMC with PID 47524 (/Users/IdeaProjects/whyisitloadingeverything/build/classes/main started in /Users/IdeaProjects/whyisitloadingeverything)
2016-02-16 11:42:21.896  INFO 47524 --- [           main] com.example.ApplicationA                 : No active profile set, falling back to default profiles: default
2016-02-16 11:42:21.975  INFO 47524 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@24a35978: startup date [Tue Feb 16 11:42:21 PST 2016]; root of context hierarchy
2016-02-16 11:42:22.883  INFO 47524 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [run-app-b-context.xml]
2016-02-16 11:42:23.081  INFO 47524 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [run-app-a-context.xml]
2016-02-16 11:42:23.255  INFO 47524 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:/Users/.gradle/caches/modules-2/files-2.1/org.springframework.integration/spring-integration-core/4.2.4.RELEASE/4780d5c2b680796844916402816135f0e79f166f/spring-integration-core-4.2.4.RELEASE.jar!/META-INF/spring.integration.default.properties]
2016-02-16 11:42:23.258  INFO 47524 --- [           main] o.s.i.config.IntegrationRegistrar        : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2016-02-16 11:42:23.451  INFO 47524 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2016-02-16 11:42:23.470  INFO 47524 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2016-02-16 11:42:24.036  INFO 47524 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:/Users/.gradle/caches/modules-2/files-2.1/org.springframework.integration/spring-integration-core/4.2.4.RELEASE/4780d5c2b680796844916402816135f0e79f166f/spring-integration-core-4.2.4.RELEASE.jar!/META-INF/spring.integration.default.properties]
2016-02-16 11:42:24.100  INFO 47524 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService  'taskScheduler'
2016-02-16 11:42:24.226  INFO 47524 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-16 11:42:24.243  INFO 47524 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-02-16 11:42:24.243  INFO 47524 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2016-02-16 11:42:24.244  INFO 47524 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 1 subscriber(s).
2016-02-16 11:42:24.244  INFO 47524 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger
2016-02-16 11:42:24.256  INFO 47524 --- [           main] com.example.ApplicationA                 : Started ApplicationA in 2.9 seconds (JVM running for 3.466)
application A
application B
2016-02-16 11:42:24.257  INFO 47524 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@24a35978: startup date [Tue Feb 16 11:42:21 PST 2016]; root of context hierarchy
2016-02-16 11:42:24.258  INFO 47524 --- [       Thread-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2016-02-16 11:42:24.259  INFO 47524 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2016-02-16 11:42:24.259  INFO 47524 --- [       Thread-1] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 0 subscriber(s).
2016-02-16 11:42:24.259  INFO 47524 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped _org.springframework.integration.errorLogger
2016-02-16 11:42:24.260  INFO 47524 --- [       Thread-1] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
2016-02-16 11:42:24.261  INFO 47524 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

BUILD SUCCESSFUL

Total time: 13.537 secs

我看到两个xml上下文文件都被加载,因此两个文件中的bean都被添加到应用程序上下文中。

为什么Spring/Spring Boot会自动加载所有xml配置文件?我该如何阻止这种行为?

共有1个答案

李和昶
2023-03-14

这是因为@springbootapplication默认包含@componentscan注释和@configuration注释。

如果未定义basePackageClasses()basePackages()(或其别名value()),则ComponentScan批注默认情况下将递归扫描当前包。它查找用@Configuration标记的类,如果找到一个类,其中定义的bean就会添加到应用程序上下文中。

因为applicationAapplicationB都在同一个包中;启动applicationA时,applicationB将自动进行组件扫描(启动applicationB时,反之亦然)。

修复方法是将applicationAapplicationB放在单独的包中。

 类似资料:
  • 创建一个同步多线程系统,以确定三个文本文件中所有整数或分数的总数。如果流被阻塞,则必须将其名称显示为"LOCKED"。为什么只计算一个文件(带锁的线程)?程序显示的内容:Thread-0 Locked!线程-2锁定!123.321 322099只有一个文件

  • 我是一个长期LAMP开发人员,并且习惯在需要时使用autoloader加载类,但直到那时才开始,但现在正在学习Node.js。许多示例显示了加载所有文件的文件夹中的index.js文件。 例如:模型 在我的代码中,我使用 这是首选方法吗? 如果是,为什么? 为什么不使用自动装载机,只在需要的时候加载文件呢? 我在网上搜索,但没有找到任何明确解释这一点的东西。如果你有一篇文章对你有帮助,我将不胜感激

  • 问题内容: 我只是想知道… 为什么我只有一个Calendar对象实例。有一个单例的原因吗? 我试图阅读文档,但他们没有提及为什么需要这样做。快速谷歌搜索没有给我任何答案。 问题答案: 日历不是单例,它是一个抽象类。该方法是一个Factory方法,它返回Calendar类的具体实现。 在Google上搜索java.util.Calendar源代码,您将看到它的工作方式。

  • 问题内容: 这最终会消耗我所有的可用内存,然后进程被杀死。我曾尝试将标签从更改为“较小”标签,但这并没有什么不同。 我在做什么错/如何处理这个大文件? 我可以轻松地将其切碎并以较小的块进行处理,但这比我想要的还要难看。 问题答案: 当遍历整个文件时,将构建一棵树,并且不会释放任何元素。这样做的好处是元素可以记住其父元素是谁,并且您可以形成引用祖先元素的XPath。缺点是它会消耗大量内存。 为了在解

  • 我的目录上有一个文件。我只需要在我的超文本标记语言页面上的按钮为用户下载此文件。 我想出了这个代码,但问题是,当我单击在我的超文本标记语言页面没有发生任何事情,也没有错误。但是文件没有被下载。 我的密码在烧瓶里 注意:我检查了所有关于堆栈溢出的问题。他们中的大多数人在下载之前把文件放在静态文件夹中,或者其他我不理解他们在做什么的人,因为他们没有在HTML部分显示要做什么

  • 我做了一个插件,可以跟踪你在《我的世界》开采的钻石数量,并保存到config.yml文件中。但是我的代码似乎不工作,我不知道为什么? 我已经在setConfig参数中尝试了1,现在我已经切换到这个,它似乎仍然不起作用。我还在我的配置.yml文件中预定义了菱形。 当玩家执行命令/mydiamonds时,它会打印出“你已经开采了(a)钻石”。但不管你开采了多少钻石,它总是打印出零。