Reputation: 15330
I have a very basic Spring Boot Application.
There are 2 @SpringBootApplication
s in the project:
ApplicationA:
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"));
}
}
ApplicationB:
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"));
}
}
As you can see, ApplicationA is only supposed to import run-app-a-context.xml
and ApplicationB is only supposed to import run-app-b-context.xml
. However, this is not what occurs.
If I run ./gradlew bootRun
; I get the following output (emphasis added):
./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
I see that both xml context files are being loaded and therefore the beans in both files are added to the Application Context.
Why is Spring/Spring Boot automatically loading all xml configuration files? How do I stop this behavior?
Upvotes: 0
Views: 1608
Reputation: 15330
It is because @SpringBootApplication
by default includes the @ComponentScan
annotation as well as the @Configuration
annotation.
The ComponentScan annotation will recursively scan the current package by default if no basePackageClasses()
or basePackages()
(or its alias value()
) is defined. It looks for classes marked with @Configuration, and if it finds one, the beans defined in it will be added to the Application Context.
Since both ApplicationA
and ApplicationB
are in the same package; when starting ApplicationA
, ApplicationB
is automatically component-scanned in (and vice-versa when starting ApplicationB
).
The fix is to put ApplicationA
and ApplicationB
in separate packages.
Upvotes: 5