JAVA-19555 Move articles from spring-core-5 module to spring-boot-annotations-2 module (#13711)
* JAVA-19555 Move articles from spring-core-5 module to spring-boot-annotations-2 module * JAVA-19555 Move articles from spring-core module to spring-boot-annotations-2 module
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [Instantiating Multiple Beans of the Same Class with Spring Annotations](https://www.baeldung.com/spring-same-class-multiple-beans)
|
||||
- [Using Environment Variables in Spring Boot’s application.properties](https://www.baeldung.com/spring-boot-properties-env-variables)
|
||||
- [Reinitialize Singleton Bean in Spring Context](https://www.baeldung.com/spring-reinitialize-singleton-bean)
|
||||
- [HTTP Interface in Spring 6](https://www.baeldung.com/spring-6-http-interface)
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
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 + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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 + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
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 {
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.multibeaninstantiation.solution3;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MultiBeanFactory<T> {
|
||||
List<T> getObject(String name) throws Exception;
|
||||
|
||||
Class<?> getObjectType();
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
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 + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
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(",");
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user