Skip to content

Commit 351f2bc

Browse files
myluckagainpedja4
authored andcommitted
BAEL-2072 (eugenp#4983)
* BAEL-2072 * rename
1 parent c1f4486 commit 351f2bc

9 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.componentscan;
2+
3+
public class ExampleBean {
4+
5+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.componentscan.springapp;
2+
3+
import org.springframework.context.annotation.FilterType;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.componentscan.ExampleBean;
7+
import com.baeldung.componentscan.springapp.flowers.Rose;
8+
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.ComponentScan;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
@Configuration
16+
@ComponentScan
17+
//@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
18+
//@ComponentScan(basePackages = "com.baeldung.componentscan.springapp")
19+
//@ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals")
20+
//@ComponentScan (excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springapp\\.flowers\\..*"))
21+
public class SpringComponentScanApp {
22+
23+
private static ApplicationContext applicationContext;
24+
25+
@Bean
26+
public ExampleBean exampleBean() {
27+
return new ExampleBean();
28+
}
29+
30+
public static void main(String[] args) {
31+
applicationContext = new AnnotationConfigApplicationContext(SpringComponentScanApp.class);
32+
33+
for (String beanName : applicationContext.getBeanDefinitionNames()) {
34+
System.out.println(beanName);
35+
}
36+
}
37+
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.componentscan.springapp.animals;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Cat {
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.componentscan.springapp.animals;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Dog {
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.componentscan.springapp.flowers;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Rose {
7+
8+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.componentscan.springbootapp.animals;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Cat {
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.componentscan.springbootapp.animals;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Dog {
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.componentscan.springbootapp.flowers;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Rose {
7+
8+
}

0 commit comments

Comments
 (0)