-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix detecting bean for @ConfigurationInject injection (#8234)
- Loading branch information
Showing
4 changed files
with
302 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...test/groovy/io/micronaut/inject/configproperties/ConfigurationPropertiesInjectSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.micronaut.inject.configproperties | ||
|
||
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec | ||
import io.micronaut.context.ApplicationContext | ||
|
||
class ConfigurationPropertiesInjectSpec extends AbstractTypeElementSpec { | ||
|
||
void "test @ConfigurationInject constructor with beans and other configs"() { | ||
given: | ||
when: | ||
def context = ApplicationContext.run(['spec': getClass().getSimpleName(), 'foo.bar.host': 'test', 'foo.bar.server-port': '123', 'xyz.name': "33"]) | ||
def config = context.getBean(MyConfigWithConstructorConfigurationInject) | ||
|
||
then: | ||
config.host == 'test' | ||
config.serverPort == 123 | ||
config.otherBean | ||
config.otherConfig.name == "33" | ||
config.otherMissingConfig | ||
config.otherSingleton | ||
config.optionalOtherSingleton.get() | ||
config.otherSingletonBeanProvider.get() == config.otherSingleton | ||
config.otherSingletonProvider.get() == config.otherSingleton | ||
|
||
cleanup: | ||
context.close() | ||
} | ||
|
||
void "test @ConfigurationInject method with beans and other configs"() { | ||
given: | ||
when: | ||
def context = ApplicationContext.run(['spec': getClass().getSimpleName(), 'foo.bar.host': 'test', 'foo.bar.server-port': '123', 'xyz.name': "33"]) | ||
def config = context.getBean(MyConfigWithMethodConfigurationInject) | ||
|
||
then: | ||
config.host == 'test' | ||
config.serverPort == 123 | ||
config.otherBean | ||
config.otherConfig.name == "33" | ||
config.otherMissingConfig | ||
config.otherSingleton | ||
config.optionalOtherSingleton.get() | ||
config.otherSingletonBeanProvider.get() == config.otherSingleton | ||
config.otherSingletonProvider.get() == config.otherSingleton | ||
|
||
cleanup: | ||
context.close() | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...oovy/io/micronaut/inject/configproperties/MyConfigWithConstructorConfigurationInject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.micronaut.inject.configproperties; | ||
|
||
import io.micronaut.context.BeanProvider; | ||
import io.micronaut.context.annotation.Bean; | ||
import io.micronaut.context.annotation.ConfigurationInject; | ||
import io.micronaut.context.annotation.ConfigurationProperties; | ||
import io.micronaut.context.annotation.Requires; | ||
import jakarta.inject.Provider; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.Optional; | ||
|
||
@Requires(property = "spec", value = "ConfigurationPropertiesInjectSpec") | ||
@ConfigurationProperties("foo.bar") | ||
class MyConfigWithConstructorConfigurationInject { | ||
private String host; | ||
private int serverPort; | ||
private MI_OtherConfig otherConfig; | ||
private MI_OtherMissingConfig otherMissingConfig; | ||
private MI_OtherBean otherBean; | ||
private MI_OtherSingleton otherSingleton; | ||
private Optional<MI_OtherSingleton> optionalOtherSingleton; | ||
private BeanProvider<MI_OtherSingleton> otherSingletonBeanProvider; | ||
private Provider<MI_OtherSingleton> otherSingletonProvider; | ||
|
||
@ConfigurationInject | ||
MyConfigWithConstructorConfigurationInject(String host, | ||
int serverPort, | ||
MI_OtherConfig otherConfig, | ||
MI_OtherMissingConfig otherMissingConfig, | ||
MI_OtherBean otherBean, | ||
MI_OtherSingleton otherSingleton, | ||
Optional<MI_OtherSingleton> optionalOtherSingleton, | ||
BeanProvider<MI_OtherSingleton> otherSingletonBeanProvider, | ||
Provider<MI_OtherSingleton> otherSingletonProvider) { | ||
this.host = host; | ||
this.serverPort = serverPort; | ||
this.otherConfig = otherConfig; | ||
this.otherMissingConfig = otherMissingConfig; | ||
this.otherBean = otherBean; | ||
this.otherSingleton = otherSingleton; | ||
this.optionalOtherSingleton = optionalOtherSingleton; | ||
this.otherSingletonBeanProvider = otherSingletonBeanProvider; | ||
this.otherSingletonProvider = otherSingletonProvider; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public int getServerPort() { | ||
return serverPort; | ||
} | ||
|
||
public MI_OtherBean getOtherBean() { | ||
return otherBean; | ||
} | ||
|
||
public MI_OtherConfig getOtherConfig() { | ||
return otherConfig; | ||
} | ||
|
||
public MI_OtherMissingConfig getOtherMissingConfig() { | ||
return otherMissingConfig; | ||
} | ||
|
||
public MI_OtherSingleton getOtherSingleton() { | ||
return otherSingleton; | ||
} | ||
|
||
public Optional<MI_OtherSingleton> getOptionalOtherSingleton() { | ||
return optionalOtherSingleton; | ||
} | ||
|
||
public BeanProvider<MI_OtherSingleton> getOtherSingletonBeanProvider() { | ||
return otherSingletonBeanProvider; | ||
} | ||
|
||
public Provider<MI_OtherSingleton> getOtherSingletonProvider() { | ||
return otherSingletonProvider; | ||
} | ||
} | ||
|
||
@ConfigurationProperties("xyz") | ||
class CI_OtherConfig { | ||
|
||
String name; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
@ConfigurationProperties("abc") | ||
class CI_OtherMissingConfig { | ||
|
||
String name; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
@Bean | ||
class CI_OtherBean { | ||
} | ||
|
||
@Singleton | ||
class CI_OtherSingleton { | ||
} |
118 changes: 118 additions & 0 deletions
118
...st/groovy/io/micronaut/inject/configproperties/MyConfigWithMethodConfigurationInject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.micronaut.inject.configproperties; | ||
|
||
import io.micronaut.context.BeanProvider; | ||
import io.micronaut.context.annotation.Bean; | ||
import io.micronaut.context.annotation.ConfigurationInject; | ||
import io.micronaut.context.annotation.ConfigurationProperties; | ||
import io.micronaut.context.annotation.Requires; | ||
import jakarta.inject.Provider; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.Optional; | ||
|
||
@Requires(property = "spec", value = "ConfigurationPropertiesInjectSpec") | ||
@ConfigurationProperties("foo.bar") | ||
class MyConfigWithMethodConfigurationInject { | ||
private String host; | ||
private int serverPort; | ||
private MI_OtherConfig otherConfig; | ||
private MI_OtherMissingConfig otherMissingConfig; | ||
private MI_OtherBean otherBean; | ||
private MI_OtherSingleton otherSingleton; | ||
private Optional<MI_OtherSingleton> optionalOtherSingleton; | ||
private BeanProvider<MI_OtherSingleton> otherSingletonBeanProvider; | ||
private Provider<MI_OtherSingleton> otherSingletonProvider; | ||
|
||
@ConfigurationInject | ||
void inject(String host, | ||
int serverPort, | ||
MI_OtherConfig otherConfig, | ||
MI_OtherMissingConfig otherMissingConfig, | ||
MI_OtherBean otherBean, | ||
MI_OtherSingleton otherSingleton, | ||
Optional<MI_OtherSingleton> optionalOtherSingleton, | ||
BeanProvider<MI_OtherSingleton> otherSingletonBeanProvider, | ||
Provider<MI_OtherSingleton> otherSingletonProvider) { | ||
this.host = host; | ||
this.serverPort = serverPort; | ||
this.otherConfig = otherConfig; | ||
this.otherMissingConfig = otherMissingConfig; | ||
this.otherBean = otherBean; | ||
this.otherSingleton = otherSingleton; | ||
this.optionalOtherSingleton = optionalOtherSingleton; | ||
this.otherSingletonBeanProvider = otherSingletonBeanProvider; | ||
this.otherSingletonProvider = otherSingletonProvider; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public int getServerPort() { | ||
return serverPort; | ||
} | ||
|
||
public MI_OtherBean getOtherBean() { | ||
return otherBean; | ||
} | ||
|
||
public MI_OtherConfig getOtherConfig() { | ||
return otherConfig; | ||
} | ||
|
||
public MI_OtherMissingConfig getOtherMissingConfig() { | ||
return otherMissingConfig; | ||
} | ||
|
||
public MI_OtherSingleton getOtherSingleton() { | ||
return otherSingleton; | ||
} | ||
|
||
public Optional<MI_OtherSingleton> getOptionalOtherSingleton() { | ||
return optionalOtherSingleton; | ||
} | ||
|
||
public BeanProvider<MI_OtherSingleton> getOtherSingletonBeanProvider() { | ||
return otherSingletonBeanProvider; | ||
} | ||
|
||
public Provider<MI_OtherSingleton> getOtherSingletonProvider() { | ||
return otherSingletonProvider; | ||
} | ||
} | ||
|
||
@ConfigurationProperties("xyz") | ||
class MI_OtherConfig { | ||
|
||
String name; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
@ConfigurationProperties("abc") | ||
class MI_OtherMissingConfig { | ||
|
||
String name; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
@Bean | ||
class MI_OtherBean { | ||
} | ||
|
||
@Singleton | ||
class MI_OtherSingleton { | ||
} |