BAEL-5449 Instantiating Multiple Beans of the Same Class with Spring Annotations (#12446)

* added multiple bean instantiation

* added module /spring-core-6 and formatting changes

* resolved formatting issues as suggested

* resolved formatting issues and changed package names

* formatting changes in XML files

* Reformatted XML files as per the instructions

* FIXED Example in Section 3 and some changes in Pom.xml

* changed xml config to Java config in solution 3

* Project updated to spring boot application

* Removed unwanted spring dependencies from pom.xml

* moved the project under new module spring-core-6

* added spring-core-6 module in parent's pom.xml

* resolved formatting issue in parent's pom.xml

* new module entry in parent's pom

* final commit

* Added spring-core-6 in parent's pom.xml 

* fixed some merging issues.

* added spring-core-6 module entry under jdk9-and-above profile in main pom

* removed App.java from multibeaninstantiation directory
This commit is contained in:
Sumit Kumar 2022-07-08 09:14:24 +05:30 committed by GitHub
parent b045e8309d
commit 2fe4ccbead
16 changed files with 345 additions and 1 deletions

View File

@ -1246,6 +1246,7 @@
<module>core-java-modules/core-java-networking-3</module>
<module>core-java-modules/core-java-strings</module>
<module>core-java-modules/core-java-httpclient</module>
<module>spring-core-6</module>
<module>ddd-contexts</module>
<module>docker-modules</module>
<module>apache-httpclient-2</module>
@ -1328,6 +1329,7 @@
<module>testing-modules/testing-assertions</module>
<module>persistence-modules/fauna</module>
<module>lightrun</module>
<module>spring-core-6</module>
</modules>
</profile>
</profiles>
@ -1400,4 +1402,4 @@
<guava.version>31.0.1-jre</guava.version>
</properties>
</project>
</project>

76
spring-core-6/pom.xml Normal file
View File

@ -0,0 +1,76 @@
<?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>
<groupId>com.baeldung</groupId>
<artifactId>spring-core-6</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-core-6</name>
<url>http://www.baeldung.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,17 @@
package com.baeldung.multibeaninstantiation.solution1;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String secondName) {
super();
this.firstName = firstName;
this.lastName = secondName;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", secondName=" + lastName + "]";
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.multibeaninstantiation.solution1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PersonConfig {
@Bean
public Person personOne() {
return new Person("Harold", "Finch");
}
@Bean
public Person personTwo() {
return new Person("John", "Reese");
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.multibeaninstantiation.solution1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringApp1 {
public static void main(String[] args) {
SpringApplication.run(SpringApp1.class, args);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.multibeaninstantiation.solution2;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String secondName) {
super();
this.firstName = firstName;
this.lastName = secondName;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", secondName=" + lastName + "]";
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.multibeaninstantiation.solution2;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.multibeaninstantiation.solution2")
public class PersonConfig {
}

View File

@ -0,0 +1,13 @@
package com.baeldung.multibeaninstantiation.solution2;
import org.springframework.stereotype.Component;
import com.baeldung.multibeaninstantiation.solution2.Person;
@Component
public class PersonOne extends Person {
public PersonOne() {
super("Harold", "Finch");
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.multibeaninstantiation.solution2;
import org.springframework.stereotype.Component;
import com.baeldung.multibeaninstantiation.solution2.Person;
@Component
public class PersonTwo extends Person {
public PersonTwo() {
super("John", "Reese");
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.multibeaninstantiation.solution2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringApp2 {
public static void main(String[] args) {
SpringApplication.run(SpringApp2.class, args);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.multibeaninstantiation.solution3;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
public class Human implements InitializingBean {
private Person personOne;
private Person personTwo;
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(personOne, "Harold is alive!");
Assert.notNull(personTwo, "John is alive!");
}
/* Setter injection */
@Autowired
public void setPersonOne(Person personOne) {
this.personOne = personOne;
this.personOne.setFirstName("Harold");
this.personOne.setSecondName("Finch");
}
@Autowired
public void setPersonTwo(Person personTwo) {
this.personTwo = personTwo;
this.personTwo.setFirstName("John");
this.personTwo.setSecondName("Reese");
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.multibeaninstantiation.solution3;
import java.util.List;
public interface MultiBeanFactory<T> {
List<T> getObject(String name) throws Exception;
Class<?> getObjectType();
}

View File

@ -0,0 +1,49 @@
package com.baeldung.multibeaninstantiation.solution3;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
@Qualifier(value = "personOne, personTwo")
public class Person implements FactoryBean<Object> {
private String firstName;
private String secondName;
public Person() {
// initialization code (optional)
}
@Override
public Class<Person> getObjectType() {
return Person.class;
}
@Override
public Object getObject() throws Exception {
return new Person();
}
public boolean isSingleton() {
return true;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", secondName=" + secondName + "]";
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.multibeaninstantiation.solution3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PersonConfig {
@Bean
public PersonFactoryPostProcessor PersonFactoryPostProcessor() {
return new PersonFactoryPostProcessor();
}
@Bean
public Person person() {
return new Person();
}
@Bean
public Human human() {
return new Human();
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.multibeaninstantiation.solution3;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class PersonFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Map<String, Object> map = beanFactory.getBeansWithAnnotation(Qualifier.class);
for (Map.Entry<String, Object> entry : map.entrySet()) {
createInstances(beanFactory, entry.getKey(), entry.getValue());
}
}
private void createInstances(ConfigurableListableBeanFactory beanFactory, String beanName, Object bean) {
Qualifier qualifier = bean.getClass()
.getAnnotation(Qualifier.class);
for (String name : extractNames(qualifier)) {
Object newBean = beanFactory.getBean(beanName);
beanFactory.registerSingleton(name.trim(), newBean);
}
}
private String[] extractNames(Qualifier qualifier) {
return qualifier.value()
.split(",");
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.multibeaninstantiation.solution3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringApp3 {
public static void main(String[] args) {
SpringApplication.run(SpringApp3.class, args);
}
}