Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pl.mperor.lab.spring.config;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Configuration
public class OrderableBeans {
@Bean
Orderable defaultPlusPriorityOrder() {
return new Orderable();
}

@Bean
@Order(0)
Orderable zeroPriorityOrder() {
return new Orderable();
}

@Bean
@Order(Integer.MIN_VALUE)
Orderable minusPriorityOrder() {
return new Orderable();
}

static class Orderable implements BeanNameAware {
String beanName;

@Override
public void setBeanName(String name) {
this.beanName = name;
}

String getBeanName() {
return beanName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package pl.mperor.lab.spring;
package pl.mperor.lab.spring.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class Beans {
public class ScopedBeans {

record Singleton() {
}
Expand All @@ -32,37 +30,6 @@ Prototype prototype() {
return new Prototype();
}

static class Orderable implements BeanNameAware {
String beanName;

@Override
public void setBeanName(String name) {
this.beanName = name;
}

String getBeanName() {
return beanName;
}
}

@Bean
@Order
Orderable plusPriorityOrder() {
return new Orderable();
}

@Bean
@Order(0)
Orderable zeroPriorityOrder() {
return new Orderable();
}

@Bean
@Order(Integer.MIN_VALUE)
Orderable minusPriorityOrder() {
return new Orderable();
}

@Bean
static BeanInstantiationTracker tracker() {
return new BeanInstantiationTracker();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
package pl.mperor.lab.spring;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class SpringCoreLabApplicationTest {

@Autowired
private ApplicationContext context;
@Autowired
private Beans.BeanInstantiationTracker tracker;
@Autowired
private List<Beans.Orderable> orderables;

@Test
public void contextLoads() {
}

@Test
public void shouldInitializeSingletonAndPrototypeScopesCorrectly() {
assertThat(context.containsBean("singleton")).isTrue();
assertThat(context.containsBean("prototype")).isTrue();

assertThat(tracker.containsInitialized("singleton")).isTrue();
assertThat(tracker.containsInitialized("prototype")).isFalse();

assertThat(context.getBean(Beans.Singleton.class)).isSameAs(context.getBean(Beans.Singleton.class));
assertThat(context.getBean(Beans.Prototype.class)).isNotSameAs(context.getBean(Beans.Prototype.class));
}

@Test
public void shouldOrderBeansByPriority() {
assertThat(orderables)
.extracting(Beans.Orderable::getBeanName)
.containsExactly("minusPriorityOrder", "zeroPriorityOrder", "plusPriorityOrder");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package pl.mperor.lab.spring.config;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class SpringBeansConfigTest {

@Autowired
private ApplicationContext context;
@Autowired
private ScopedBeans.BeanInstantiationTracker tracker;

@Qualifier("defaultPlusPriorityOrder")
@Autowired
private OrderableBeans.Orderable defaultOrder;
@Autowired
private List<OrderableBeans.Orderable> orderables;

@Test
public void shouldInitializeSingletonAndPrototypeScopesCorrectly() {
assertThat(context.containsBean("singleton")).isTrue();
assertThat(context.containsBean("prototype")).isTrue();

assertThat(tracker.containsInitialized("singleton")).isTrue();
assertThat(tracker.containsInitialized("prototype")).isFalse();

assertThat(context.getBean(ScopedBeans.Singleton.class)).isSameAs(context.getBean(ScopedBeans.Singleton.class));
assertThat(context.getBean(ScopedBeans.Prototype.class)).isNotSameAs(context.getBean(ScopedBeans.Prototype.class));
}

@Test
public void shouldOrderBeansByPriority() {
assertThat(defaultOrder)
.extracting(OrderableBeans.Orderable::getBeanName)
.isEqualTo("defaultPlusPriorityOrder");

assertThat(orderables)
.extracting(OrderableBeans.Orderable::getBeanName)
.containsExactly("minusPriorityOrder", "zeroPriorityOrder", "defaultPlusPriorityOrder");
}

}