Skip to content

Commit

Permalink
Backported test for @Autowired @bean method on configuration subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jun 17, 2024
1 parent 2eed02e commit 6b62b93
Showing 1 changed file with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -44,6 +45,7 @@
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

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

Expand Down Expand Up @@ -92,8 +94,8 @@ void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
OptionalAutowiredMethodConfig.class);

assertThat(context.getBeansOfType(Colour.class).isEmpty()).isTrue();
assertThat(context.getBean(TestBean.class).getName()).isEqualTo("");
assertThat(context.getBeansOfType(Colour.class)).isEmpty();
assertThat(context.getBean(TestBean.class).getName()).isEmpty();
context.close();
}

Expand Down Expand Up @@ -184,14 +186,22 @@ void testValueInjectionWithProviderMethodArguments() {
context.close();
}

@Test
void testValueInjectionWithAccidentalAutowiredAnnotations() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ValueConfigWithAccidentalAutowiredAnnotations.class);
doTestValueInjection(context);
context.close();
}

private void doTestValueInjection(BeanFactory context) {
System.clearProperty("myProp");

TestBean testBean = context.getBean("testBean", TestBean.class);
assertThat((Object) testBean.getName()).isNull();
assertThat(testBean.getName()).isNull();

testBean = context.getBean("testBean2", TestBean.class);
assertThat((Object) testBean.getName()).isNull();
assertThat(testBean.getName()).isNull();

System.setProperty("myProp", "foo");

Expand All @@ -204,10 +214,10 @@ private void doTestValueInjection(BeanFactory context) {
System.clearProperty("myProp");

testBean = context.getBean("testBean", TestBean.class);
assertThat((Object) testBean.getName()).isNull();
assertThat(testBean.getName()).isNull();

testBean = context.getBean("testBean2", TestBean.class);
assertThat((Object) testBean.getName()).isNull();
assertThat(testBean.getName()).isNull();
}

@Test
Expand Down Expand Up @@ -271,7 +281,7 @@ public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours
return new TestBean("");
}
else {
return new TestBean(colour.get().toString() + "-" + colours.get().get(0).toString());
return new TestBean(colour.get() + "-" + colours.get().get(0).toString());
}
}
}
Expand Down Expand Up @@ -484,6 +494,32 @@ public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String>
}


@Configuration
static class ValueConfigWithAccidentalAutowiredAnnotations implements InitializingBean {

boolean invoked;

@Override
public void afterPropertiesSet() {
Assert.state(!invoked, "Factory method must not get invoked on startup");
}

@Bean @Scope("prototype")
@Autowired
public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider<String> name) {
invoked = true;
return new TestBean(name.get());
}

@Bean @Scope("prototype")
@Autowired
public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String> name2) {
invoked = true;
return new TestBean(name2.get());
}
}


@Configuration
static class PropertiesConfig {

Expand Down

0 comments on commit 6b62b93

Please sign in to comment.