Skip to content

Commit 7d65b1f

Browse files
kumar-devendermaibin
authored andcommitted
BAEL-3286 added component scan filter example (eugenp#7786)
* BAEL-3286 added component scan filter example * BAEL-3286 added component scan filter example * Fix alingemnt * Remove unused class * Corrected formatting * formatted code
1 parent ac8c5ef commit 7d65b1f

21 files changed

+234
-51
lines changed

spring-boot-di/pom.xml

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
6-
<artifactId>spring-boot-di</artifactId>
7-
<name>spring-boot-di</name>
8-
<packaging>jar</packaging>
9-
<description>Module For Spring Boot DI</description>
10-
11-
<parent>
12-
<artifactId>parent-boot-2</artifactId>
13-
<groupId>com.baeldung</groupId>
14-
<version>0.0.1-SNAPSHOT</version>
15-
<relativePath>../parent-boot-2</relativePath>
16-
</parent>
17-
18-
<dependencies>
19-
20-
<dependency>
21-
<groupId>org.springframework.boot</groupId>
22-
<artifactId>spring-boot-starter-web</artifactId>
23-
</dependency>
24-
<dependency>
25-
<groupId>org.springframework.boot</groupId>
26-
<artifactId>spring-boot-starter-tomcat</artifactId>
27-
<scope>provided</scope>
28-
</dependency>
29-
30-
<dependency>
31-
<groupId>org.apache.tomcat.embed</groupId>
32-
<artifactId>tomcat-embed-jasper</artifactId>
33-
<scope>provided</scope>
34-
</dependency>
35-
36-
</dependencies>
37-
38-
<build>
39-
<plugins>
40-
<plugin>
41-
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-maven-plugin</artifactId>
43-
<configuration>
44-
<mainClass>com.baeldung.SpringBootDiApplication</mainClass>
45-
<layout>JAR</layout>
46-
</configuration>
47-
</plugin>
48-
</plugins>
49-
</build>
50-
51-
<properties>
52-
<start-class>com.baeldung.SpringBootDiApplication</start-class>
53-
</properties>
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-boot-di</artifactId>
7+
<name>spring-boot-di</name>
8+
<packaging>jar</packaging>
9+
<description>Module For Spring Boot DI</description>
10+
11+
<parent>
12+
<artifactId>parent-boot-2</artifactId>
13+
<groupId>com.baeldung</groupId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<relativePath>../parent-boot-2</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.aspectj</groupId>
21+
<artifactId>aspectjweaver</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-tomcat</artifactId>
31+
<scope>provided</scope>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.apache.tomcat.embed</groupId>
36+
<artifactId>tomcat-embed-jasper</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
<configuration>
48+
<mainClass>com.baeldung.SpringBootDiApplication</mainClass>
49+
<layout>JAR</layout>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<properties>
56+
<start-class>com.baeldung.SpringBootDiApplication</start-class>
57+
</properties>
5458

5559
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.componentscan.filter.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface Animal { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.componentscan.filter.annotation;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.FilterType;
8+
9+
import java.util.Arrays;
10+
11+
@Configuration
12+
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Animal.class))
13+
public class ComponentScanAnnotationFilterApp {
14+
private static ApplicationContext applicationContext;
15+
16+
public static void main(String[] args) {
17+
applicationContext = new AnnotationConfigApplicationContext(ComponentScanAnnotationFilterApp.class);
18+
Arrays.stream(applicationContext.getBeanDefinitionNames())
19+
.forEach(System.out::println);
20+
}
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.componentscan.filter.annotation;
2+
3+
@Animal
4+
public class Elephant { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.componentscan.filter.aspectj;
2+
3+
public class Cat { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.componentscan.filter.aspectj;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.FilterType;
8+
9+
import java.util.Arrays;
10+
11+
@Configuration
12+
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ,
13+
pattern = "com.baeldung.componentscan.filter.aspectj.* "
14+
+ "&& !(com.baeldung.componentscan.filter.aspectj.L* "
15+
+ "|| com.baeldung.componentscan.filter.aspectj.E*)"))
16+
public class ComponentScanCustomFilterApp {
17+
private static ApplicationContext applicationContext;
18+
19+
public static void main(String[] args) {
20+
applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class);
21+
Arrays.stream(applicationContext.getBeanDefinitionNames())
22+
.forEach(System.out::println);
23+
}
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.componentscan.filter.aspectj;
2+
3+
public class Elephant { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.componentscan.filter.aspectj;
2+
3+
public class Loin { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.componentscan.filter.assignable;
2+
3+
public interface Animal { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.componentscan.filter.assignable;
2+
3+
public class Cat implements Animal { }

0 commit comments

Comments
 (0)