BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
## Spring Boot Dependency Inject
This module contains articles about dependency injection with Spring Boot
### The Course
The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles:
- [Spring Component Scanning](https://www.baeldung.com/spring-component-scanning)
+64
View File
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-di</artifactId>
<name>spring-boot-di</name>
<packaging>jar</packaging>
<description>Module For Spring Boot DI</description>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.SpringBootDiApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.baeldung.SpringBootDiApplication</start-class>
</properties>
</project>
@@ -0,0 +1,12 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDiApplication.class, args);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.componentscan;
public class ExampleBean {
}
@@ -0,0 +1,11 @@
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 {
}
@@ -0,0 +1,13 @@
package com.baeldung.componentscan.filter.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Animal.class))
public class ComponentScanAnnotationFilterApp {
public static void main(String[] args) {
}
}
@@ -0,0 +1,5 @@
package com.baeldung.componentscan.filter.annotation;
@Animal
public class Elephant {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.aspectj;
public class Cat {
}
@@ -0,0 +1,15 @@
package com.baeldung.componentscan.filter.aspectj;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@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.C*)"))
public class ComponentScanAspectJFilterApp {
public static void main(String[] args) {
}
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.aspectj;
public class Elephant {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.aspectj;
public class Loin {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.assignable;
public interface Animal {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.assignable;
public class Cat implements Animal {
}
@@ -0,0 +1,13 @@
package com.baeldung.componentscan.filter.assignable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Animal.class))
public class ComponentScanAssignableTypeFilterApp {
public static void main(String[] args) {
}
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.assignable;
public class Elephant implements Animal {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.custom;
public class Cat {
}
@@ -0,0 +1,19 @@
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 fullyQualifiedName = classMetadata.getClassName();
String className = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(".") + 1);
return className.length() > 5 ? true : false;
}
}
@@ -0,0 +1,13 @@
package com.baeldung.componentscan.filter.custom;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = ComponentScanCustomFilter.class))
public class ComponentScanCustomFilterApp {
public static void main(String[] args) {
}
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.custom;
public class Elephant {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.custom;
public class Loin {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.regex;
public class Cat {
}
@@ -0,0 +1,13 @@
package com.baeldung.componentscan.filter.regex;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*(nt)"))
public class ComponentScanRegexFilterApp {
public static void main(String[] args) {
}
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.regex;
public class Elephant {
}
@@ -0,0 +1,4 @@
package com.baeldung.componentscan.filter.regex;
public class Loin {
}
@@ -0,0 +1,34 @@
package com.baeldung.componentscan.springapp;
import com.baeldung.componentscan.ExampleBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
// @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp")
// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals")
// @ComponentScan (excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springapp\\.flowers\\..*"))
public class SpringComponentScanApp {
private static ApplicationContext applicationContext;
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(SpringComponentScanApp.class);
for (String beanName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Cat {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Dog {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springapp.flowers;
import org.springframework.stereotype.Component;
@Component
public class Rose {
}
@@ -0,0 +1,34 @@
package com.baeldung.componentscan.springbootapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import com.baeldung.componentscan.ExampleBean;
@SpringBootApplication
// @ComponentScan(basePackages = "com.baeldung.componentscan.springbootapp.animals")
// @ComponentScan ( excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springbootapp\\.flowers\\..*"))
// @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
public class SpringBootComponentScanApp {
private static ApplicationContext applicationContext;
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
public static void main(String[] args) {
applicationContext = SpringApplication.run(SpringBootComponentScanApp.class, args);
checkBeansPresence("cat", "dog", "rose", "exampleBean", "springBootApp");
}
private static void checkBeansPresence(String... beans) {
for (String beanName : beans) {
System.out.println("Is " + beanName + " in ApplicationContext: " + applicationContext.containsBean(beanName));
}
}
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springbootapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Cat {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springbootapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Dog {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springbootapp.flowers;
import org.springframework.stereotype.Component;
@Component
public class Rose {
}
@@ -0,0 +1,30 @@
package com.baeldung.componentscan.filter.annotation;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentScanAnnotationFilterApp.class)
public class ComponentScanAnnotationFilterAppIntegrationTest {
@Test
public void whenAnnotationFilterIsUsed_thenComponentScanShouldRegisterBeanAnnotatedWithAnimalAnootation() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanAnnotationFilterApp.class);
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanAnnotationFilterApp"))
.collect(Collectors.toList());
assertThat(beans.size(), equalTo(1));
assertThat(beans.get(0), equalTo("elephant"));
}
}
@@ -0,0 +1,30 @@
package com.baeldung.componentscan.filter.aspectj;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentScanAspectJFilterApp.class)
public class ComponentScanAspectJFilterAppIntegrationTest {
@Test
public void whenAspectJFilterIsUsed_thenComponentScanShouldRegisterBeanMatchingAspectJCreteria() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanAspectJFilterApp.class);
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanAspectJFilterApp"))
.collect(Collectors.toList());
assertThat(beans.size(), equalTo(1));
assertThat(beans.get(0), equalTo("elephant"));
}
}
@@ -0,0 +1,31 @@
package com.baeldung.componentscan.filter.assignable;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentScanAssignableTypeFilterApp.class)
public class ComponentScanAssignableTypeFilterAppIntegrationTest {
@Test
public void whenAssignableTypeFilterIsUsed_thenComponentScanShouldRegisterBeanOfAssignableTypeAndItsSubClass() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanAssignableTypeFilterApp.class);
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanAssignableTypeFilterApp"))
.collect(Collectors.toList());
assertThat(beans.size(), equalTo(2));
assertThat(beans.contains("cat"), equalTo(true));
assertThat(beans.contains("elephant"), equalTo(true));
}
}
@@ -0,0 +1,31 @@
package com.baeldung.componentscan.filter.custom;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentScanCustomFilterApp.class)
public class ComponentScanCustomFilterAppIntegrationTest {
@Test
public void whenCustomFilterIsUsed_thenComponentScanShouldRegisterBeanMatchingCustomFilter() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomFilterApp.class);
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanCustomFilterApp")
&& !bean.contains("componentScanCustomFilter"))
.collect(Collectors.toList());
assertThat(beans.size(), equalTo(1));
assertThat(beans.get(0), equalTo("elephant"));
}
}
@@ -0,0 +1,30 @@
package com.baeldung.componentscan.filter.regex;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentScanRegexFilterApp.class)
public class ComponentScanRegexFilterAppIntegrationTest {
@Test
public void whenRegexFilterIsUsed_thenComponentScanShouldRegisterBeanMatchingRegex() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanRegexFilterApp.class);
List<String> beans = Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(bean -> !bean.contains("org.springframework") && !bean.contains("componentScanRegexFilterApp"))
.collect(Collectors.toList());
assertThat(beans.size(), equalTo(1));
assertThat(beans.contains("elephant"), equalTo(true));
}
}