diff --git a/spring-boot-di/pom.xml b/spring-boot-di/pom.xml index b07d7c6385..c0faf44335 100644 --- a/spring-boot-di/pom.xml +++ b/spring-boot-di/pom.xml @@ -1,55 +1,59 @@ - 4.0.0 - spring-boot-di - spring-boot-di - jar - Module For Spring Boot DI + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + spring-boot-di + spring-boot-di + jar + Module For Spring Boot DI - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 - + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + - + + + org.aspectj + aspectjweaver + - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-tomcat - provided - + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + provided + - - org.apache.tomcat.embed - tomcat-embed-jasper - provided - + + org.apache.tomcat.embed + tomcat-embed-jasper + provided + - + - - - - org.springframework.boot - spring-boot-maven-plugin - - com.baeldung.SpringBootDiApplication - JAR - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.SpringBootDiApplication + JAR + + + + - - com.baeldung.SpringBootDiApplication - + + com.baeldung.SpringBootDiApplication + diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java new file mode 100644 index 0000000000..73bd0e3673 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java @@ -0,0 +1,10 @@ +package com.baeldung.componentscan.filter.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Animal { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java new file mode 100644 index 0000000000..7e89870d4b --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java @@ -0,0 +1,21 @@ +package com.baeldung.componentscan.filter.annotation; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +import java.util.Arrays; + +@Configuration +@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Animal.class)) +public class ComponentScanAnnotationFilterApp { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = new AnnotationConfigApplicationContext(ComponentScanAnnotationFilterApp.class); + Arrays.stream(applicationContext.getBeanDefinitionNames()) + .forEach(System.out::println); + } +} diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java new file mode 100644 index 0000000000..8ad8111d6b --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java @@ -0,0 +1,4 @@ +package com.baeldung.componentscan.filter.annotation; + +@Animal +public class Elephant { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Cat.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Cat.java new file mode 100644 index 0000000000..b34a2d7112 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Cat.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.aspectj; + +public class Cat { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/ComponentScanCustomFilterApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/ComponentScanCustomFilterApp.java new file mode 100644 index 0000000000..3674c09531 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/ComponentScanCustomFilterApp.java @@ -0,0 +1,24 @@ +package com.baeldung.componentscan.filter.aspectj; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +import java.util.Arrays; + +@Configuration +@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, + pattern = "com.baeldung.componentscan.filter.aspectj.* " + + "&& !(com.baeldung.componentscan.filter.aspectj.L* " + + "|| com.baeldung.componentscan.filter.aspectj.E*)")) +public class ComponentScanCustomFilterApp { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class); + Arrays.stream(applicationContext.getBeanDefinitionNames()) + .forEach(System.out::println); + } +} diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Elephant.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Elephant.java new file mode 100644 index 0000000000..30abc9dcd4 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Elephant.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.aspectj; + +public class Elephant { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Loin.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Loin.java new file mode 100644 index 0000000000..cf442e981e --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/aspectj/Loin.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.aspectj; + +public class Loin { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Animal.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Animal.java new file mode 100644 index 0000000000..77cf2e72f0 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Animal.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.assignable; + +public interface Animal { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Cat.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Cat.java new file mode 100644 index 0000000000..262ae154f8 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Cat.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.assignable; + +public class Cat implements Animal { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/ComponentScanAssignableTypeFilterApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/ComponentScanAssignableTypeFilterApp.java new file mode 100644 index 0000000000..a5fa2b0942 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/ComponentScanAssignableTypeFilterApp.java @@ -0,0 +1,21 @@ +package com.baeldung.componentscan.filter.assignable; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +import java.util.Arrays; + +@Configuration +@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Animal.class)) +public class ComponentScanAssignableTypeFilterApp { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = new AnnotationConfigApplicationContext(ComponentScanAssignableTypeFilterApp.class); + Arrays.stream(applicationContext.getBeanDefinitionNames()) + .forEach(System.out::println); + } +} diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Elephant.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Elephant.java new file mode 100644 index 0000000000..815e0d762a --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/assignable/Elephant.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.assignable; + +public class Elephant implements Animal { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Cat.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Cat.java new file mode 100644 index 0000000000..282d6bb641 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Cat.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.custom; + +public class Cat extends Pet { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/ComponentScanCustomFilter.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/ComponentScanCustomFilter.java new file mode 100644 index 0000000000..ebaccf7838 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/ComponentScanCustomFilter.java @@ -0,0 +1,22 @@ +package com.baeldung.componentscan.filter.custom; + +import org.springframework.core.type.ClassMetadata; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.TypeFilter; + +import java.io.IOException; + +public class ComponentScanCustomFilter implements TypeFilter { + + @Override + public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) + throws IOException { + ClassMetadata classMetadata = metadataReader.getClassMetadata(); + String superClass = classMetadata.getSuperClassName(); + if (Pet.class.getName().equalsIgnoreCase(superClass)) { + return true; + } + return false; + } +} diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/ComponentScanCustomFilterApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/ComponentScanCustomFilterApp.java new file mode 100644 index 0000000000..1a4f5dbf4e --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/ComponentScanCustomFilterApp.java @@ -0,0 +1,21 @@ +package com.baeldung.componentscan.filter.custom; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +import java.util.Arrays; + +@Configuration +@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = ComponentScanCustomFilter.class)) +public class ComponentScanCustomFilterApp { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class); + Arrays.stream(applicationContext.getBeanDefinitionNames()) + .forEach(System.out::println); + } +} diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Loin.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Loin.java new file mode 100644 index 0000000000..0e2f9e0692 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Loin.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.custom; + +public class Loin { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Pet.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Pet.java new file mode 100644 index 0000000000..9b4497221d --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/custom/Pet.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.custom; + +public class Pet { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Cat.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Cat.java new file mode 100644 index 0000000000..87341cae9d --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Cat.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.regex; + +public class Cat { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/ComponentScanRegexFilterApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/ComponentScanRegexFilterApp.java new file mode 100644 index 0000000000..36e94446b2 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/ComponentScanRegexFilterApp.java @@ -0,0 +1,20 @@ +package com.baeldung.componentscan.filter.regex; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +import java.util.Arrays; + +@Configuration +@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*[t]")) +public class ComponentScanRegexFilterApp { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = new AnnotationConfigApplicationContext(ComponentScanRegexFilterApp.class); + Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println); + } +} diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Elephant.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Elephant.java new file mode 100644 index 0000000000..314dca5b77 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Elephant.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.regex; + +public class Elephant { } diff --git a/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Loin.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Loin.java new file mode 100644 index 0000000000..1b75fc1b43 --- /dev/null +++ b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/regex/Loin.java @@ -0,0 +1,3 @@ +package com.baeldung.componentscan.filter.regex; + +public class Loin { }