|
| 1 | +package com.baeldung.componentscan.springbootapp; |
| 2 | + |
| 3 | +import org.springframework.boot.SpringApplication; |
| 4 | +import org.springframework.context.annotation.FilterType; |
| 5 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 6 | +import org.springframework.context.ApplicationContext; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.ComponentScan; |
| 9 | + |
| 10 | +import com.baeldung.componentscan.ExampleBean; |
| 11 | +import com.baeldung.componentscan.springbootapp.flowers.Rose; |
| 12 | + |
| 13 | +@SpringBootApplication |
| 14 | +//@ComponentScan(basePackages = "com.baeldung.componentscan.springbootapp.animals") |
| 15 | +//@ComponentScan ( excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springbootapp\\.flowers\\..*")) |
| 16 | +//@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class)) |
| 17 | + |
| 18 | +public class SpringBootComponentScanApp { |
| 19 | + private static ApplicationContext applicationContext; |
| 20 | + |
| 21 | + @Bean |
| 22 | + public ExampleBean exampleBean() { |
| 23 | + return new ExampleBean(); |
| 24 | + } |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + applicationContext = SpringApplication.run(SpringBootComponentScanApp.class, args); |
| 28 | + checkBeansPresence("cat", "dog", "rose", "exampleBean", "springBootApp"); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + private static void checkBeansPresence(String... beans) { |
| 33 | + for (String beanName : beans) { |
| 34 | + System.out.println("Is " + beanName + " in ApplicationContext: " + applicationContext.containsBean(beanName)); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
0 commit comments