BAEL-6718: Setting a Spring Bean to Null (#15242)
* BAEL-6718: Setting a Spring Bean to Null * BAEL-6718: Java Nullable Example
This commit is contained in:
parent
80da8b957d
commit
3371e4fe53
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.nullablebean;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MainComponent {
|
||||
|
||||
private SubComponent subComponent;
|
||||
|
||||
public MainComponent(final SubComponent subComponent) {
|
||||
this.subComponent = subComponent;
|
||||
}
|
||||
|
||||
public SubComponent getSubComponent() {
|
||||
return subComponent;
|
||||
}
|
||||
|
||||
public void setSubComponent(final SubComponent subComponent) {
|
||||
this.subComponent = subComponent;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.nullablebean;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SubComponent {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.nullablebean.nonrequired;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan
|
||||
public class NonRequiredConfiguration {
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.nullablebean.nonrequired;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class NonRequiredMainComponent {
|
||||
|
||||
@Autowired(required = false)
|
||||
private NonRequiredSubComponent subComponent;
|
||||
|
||||
public NonRequiredSubComponent getSubComponent() {
|
||||
return subComponent;
|
||||
}
|
||||
|
||||
public void setSubComponent(final NonRequiredSubComponent subComponent) {
|
||||
this.subComponent = subComponent;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.nullablebean.nonrequired;
|
||||
|
||||
public class NonRequiredSubComponent {
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.nullablebean.nullablejava;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class NullableJavaConfiguration {
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.nullablebean.nullablejava;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class NullableMainComponent {
|
||||
|
||||
private NullableSubComponent subComponent;
|
||||
|
||||
public NullableMainComponent(final @Nullable NullableSubComponent subComponent) {
|
||||
this.subComponent = subComponent;
|
||||
}
|
||||
|
||||
public NullableSubComponent getSubComponent() {
|
||||
return subComponent;
|
||||
}
|
||||
|
||||
public void setSubComponent(final NullableSubComponent subComponent) {
|
||||
this.subComponent = subComponent;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.nullablebean.nullablejava;
|
||||
|
||||
public class NullableSubComponent {
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.nullablebean.nullablespring;
|
||||
|
||||
import com.baeldung.nullablebean.MainComponent;
|
||||
import com.baeldung.nullablebean.SubComponent;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class NullableConfiguration {
|
||||
|
||||
@Bean
|
||||
public MainComponent mainComponent(SubComponent subComponent) {
|
||||
return new MainComponent(subComponent);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SubComponent subComponent() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.nullablebean.nullablespring;
|
||||
|
||||
import com.baeldung.nullablebean.MainComponent;
|
||||
import com.baeldung.nullablebean.SubComponent;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class NullableSupplierConfiguration {
|
||||
|
||||
@Bean
|
||||
public MainComponent mainComponent(Supplier<SubComponent> subComponentSupplier) {
|
||||
return new MainComponent(subComponentSupplier.get());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Supplier<SubComponent> subComponentSupplier() {
|
||||
return () -> null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.nullablebean.optionable;
|
||||
|
||||
import com.baeldung.nullablebean.MainComponent;
|
||||
import com.baeldung.nullablebean.SubComponent;
|
||||
import java.util.Optional;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class OptionableConfiguration {
|
||||
|
||||
@Bean
|
||||
public MainComponent mainComponent(Optional<SubComponent> optionalSubComponent) {
|
||||
return new MainComponent(optionalSubComponent.orElse(null));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
package com.baeldung.nullablebean;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.baeldung.nullablebean.nonrequired.NonRequiredConfiguration;
|
||||
import com.baeldung.nullablebean.nonrequired.NonRequiredMainComponent;
|
||||
import com.baeldung.nullablebean.nullablejava.NullableJavaConfiguration;
|
||||
import com.baeldung.nullablebean.nullablejava.NullableMainComponent;
|
||||
import com.baeldung.nullablebean.nullablespring.NullableConfiguration;
|
||||
import com.baeldung.nullablebean.nullablespring.NullableSupplierConfiguration;
|
||||
import com.baeldung.nullablebean.optionable.OptionableConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
class NullableXMLComponentUnitTest {
|
||||
|
||||
@Test
|
||||
void givenContextWhenCreatingNullableMainComponentThenSubComponentIsNull() {
|
||||
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
NullableJavaConfiguration.class);
|
||||
final NullableMainComponent bean = context.getBean(NullableMainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
@Test
|
||||
void givenNonRequiredContextWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
NonRequiredConfiguration.class);
|
||||
final NonRequiredMainComponent bean = context.getBean(NonRequiredMainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOptionableContextWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
OptionableConfiguration.class);
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullableSupplierContextWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
NullableSupplierConfiguration.class);
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullableContextWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
assertThrows(UnsatisfiedDependencyException.class, () -> new AnnotationConfigApplicationContext(
|
||||
NullableConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullableXMLContextWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"nullable-application-context.xml");
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullableSpELXMLContextWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"nullable-spel-application-context.xml");
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullableSpELXMLContextWithNullablePropertiesWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"nullable-configurable-spel-application-context.xml");
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullableSpELXMLContextWithNonNullablePropertiesWhenCreatingMainComponentThenSubComponentIsNull() {
|
||||
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"non-nullable-configurable-spel-application-context.xml");
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNotNull(bean.getSubComponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenXMLContextWhenCreatingMainComponentThenSubComponentNotNull() {
|
||||
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"non-nullable-application-context.xml");
|
||||
final MainComponent bean = context.getBean(MainComponent.class);
|
||||
assertNotNull(bean.getSubComponent());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
|
||||
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
<bean class="com.baeldung.nullablebean.MainComponent" name="mainComponent">
|
||||
<constructor-arg ref="subComponent"/>
|
||||
</bean>
|
||||
<bean class="com.baeldung.nullablebean.SubComponent" name="subComponent"/>
|
||||
</beans>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
|
||||
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
<bean id="propertyConfigurer"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="classpath:non-nullable.properties"/>
|
||||
</bean>
|
||||
<bean class="com.baeldung.nullablebean.MainComponent" name="mainComponent">
|
||||
<constructor-arg value="#{ ${nullableBean} }"/>
|
||||
</bean>
|
||||
<bean class="com.baeldung.nullablebean.SubComponent" name="subComponent"/>
|
||||
</beans>
|
|
@ -0,0 +1 @@
|
|||
nullableBean = subComponent
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
|
||||
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
<bean class="com.baeldung.nullablebean.MainComponent" name="mainComponent">
|
||||
<constructor-arg>
|
||||
<null/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</beans>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
|
||||
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
<bean id="propertyConfigurer"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="classpath:nullable.properties"/>
|
||||
</bean>
|
||||
<bean class="com.baeldung.nullablebean.MainComponent" name="mainComponent">
|
||||
<constructor-arg value="#{ ${nullableBean} }"/>
|
||||
</bean>
|
||||
<bean class="com.baeldung.nullablebean.SubComponent" name="subComponent"/>
|
||||
</beans>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
|
||||
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
<bean class="com.baeldung.nullablebean.MainComponent" name="mainComponent">
|
||||
<constructor-arg value="#{null}"/>
|
||||
</bean>
|
||||
</beans>
|
|
@ -0,0 +1 @@
|
|||
nullableBean = null
|
Loading…
Reference in New Issue