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:
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.aliasfor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@RequestMapping
|
||||
public @interface MyMapping {
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "method")
|
||||
RequestMethod[] action() default {};
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||
String[] value() default {};
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||
String[] mapping() default {};
|
||||
|
||||
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||
String[] route() default {};
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.aliasfor;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class MyMappingController {
|
||||
|
||||
@MyMapping(action = RequestMethod.PATCH, route = "/test")
|
||||
public void mappingMethod() {
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.lazy;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
@Lazy
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.lazy")
|
||||
public class AppConfig {
|
||||
|
||||
@Lazy
|
||||
@Bean
|
||||
public Region getRegion(){
|
||||
return new Region();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Country getCountry(){
|
||||
return new Country();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.lazy;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class City {
|
||||
|
||||
public City() {
|
||||
System.out.println("City bean initialized");
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.lazy;
|
||||
|
||||
public class Country {
|
||||
|
||||
public Country() {
|
||||
System.out.println("Country bean initialized");
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.lazy;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
public class Region {
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private City city;
|
||||
|
||||
public Region() {
|
||||
System.out.println("Region bean initialized");
|
||||
}
|
||||
|
||||
public City getCityInstance() {
|
||||
return city;
|
||||
}
|
||||
}
|
||||
+17
@@ -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 + "]";
|
||||
}
|
||||
}
|
||||
+17
@@ -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");
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+17
@@ -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 + "]";
|
||||
}
|
||||
}
|
||||
+10
@@ -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 {
|
||||
|
||||
}
|
||||
+13
@@ -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");
|
||||
}
|
||||
}
|
||||
+13
@@ -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");
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+33
@@ -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");
|
||||
}
|
||||
}
|
||||
+9
@@ -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();
|
||||
}
|
||||
+49
@@ -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 + "]";
|
||||
}
|
||||
}
|
||||
+22
@@ -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();
|
||||
}
|
||||
}
|
||||
+33
@@ -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(",");
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.springbean.naming.component;
|
||||
|
||||
public interface Animal {
|
||||
String name();
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.springbean.naming.component;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Qualifier("cat")
|
||||
public class Cat implements Animal {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "Cat";
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.springbean.naming.component;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("myBean")
|
||||
public class CustomComponent {
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.springbean.naming.component;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Qualifier("dog")
|
||||
public class Dog implements Animal {
|
||||
@Override
|
||||
public String name() {
|
||||
return "Dog";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.springbean.naming.configuration;
|
||||
|
||||
import com.baeldung.springbean.naming.service.AuditService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class AuditConfiguration {
|
||||
|
||||
@Bean
|
||||
public AuditService audit() {
|
||||
return new AuditService();
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.springbean.naming.configuration;
|
||||
|
||||
import com.baeldung.springbean.naming.component.CustomComponent;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration("configuration")
|
||||
public class MyConfiguration {
|
||||
|
||||
@Bean("qualifierComponent")
|
||||
public CustomComponent component() {
|
||||
return new CustomComponent();
|
||||
}
|
||||
|
||||
@Bean("beanComponent")
|
||||
public CustomComponent myComponent() {
|
||||
return new CustomComponent();
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.springbean.naming.controller;
|
||||
|
||||
import com.baeldung.springbean.naming.service.MessagingService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@Controller
|
||||
public class MessagingController {
|
||||
|
||||
@Autowired
|
||||
private MessagingService service;
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.springbean.naming.service;
|
||||
|
||||
public class AuditService {
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.springbean.naming.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class LoggingService {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.springbean.naming.service;
|
||||
|
||||
public interface MessagingService {
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.springbean.naming.service;
|
||||
|
||||
import com.baeldung.springbean.naming.component.CustomComponent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MessagingServiceImpl implements MessagingService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("qualifierComponent")
|
||||
private CustomComponent customComponent;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.springbean.naming.service;
|
||||
|
||||
import com.baeldung.springbean.naming.component.Animal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PetShow {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("dog")
|
||||
private Animal dog;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("cat")
|
||||
private Animal cat;
|
||||
|
||||
public Animal getDog() {
|
||||
return dog;
|
||||
}
|
||||
|
||||
public Animal getCat() {
|
||||
return cat;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user