Comments (4)
When you specify the replacement bean using @Singleton
I would assume it would then remove all non-singletons of said type and thus replace your qualified bean.
Also in this example, what benefit is there to using @Replaces
? If you need a specific instance of this type that you can access without using qualifiers (ie a default) you might be best suited using @Primary
in this example, assuming that is the intention of the replacing going on
from micronaut-core.
not sure if what I described above is your use case, but if so this along the lines of what I was describing
import io.micronaut.context.annotation.*;
import jakarta.inject.Inject;
import jakarta.inject.Named;
@Factory
@Prototype
class MyFactory {
@Named("blah")
@Prototype
String blah() {
return "blah";
}
@Bean
@Primary
String primary() {
return "primary";
}
}
@Prototype
class MyClass {
String myString;
@Inject
public MyClass(@Named("blah") String s) {
myString = s;
}
public String getMyString(){
return myString;
}
}
@Prototype
class MyClassTwo{
String myString;
@Inject
public MyClassTwo(String s){
myString = s;
}
public String getMyString(){
return myString;
}
}
import io.micronaut.inject.qualifiers.Qualifiers
import io.micronaut.runtime.EmbeddedApplication
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification
@MicronautTest
class ExampleSpec extends Specification{
@Inject
EmbeddedApplication<?> application
def 'named bean can be found'(){
when:
def bean = application.getApplicationContext().getBean(String.class, Qualifiers.byName('blah'))
then:
bean == 'blah'
}
def 'when no qualifier return primary'(){
when:
def bean = application.getApplicationContext().getBean(String.class)
then:
bean == 'primary'
}
def 'classes that inject can use the named or the primary'(){
when:
def myClass = application.getApplicationContext().getBean(MyClass.class)
def myClassTwo = application.getApplicationContext().getBean(MyClassTwo.class)
then:
myClass.getMyString() == 'blah'
myClassTwo.getMyString() == 'primary'
}
}
from micronaut-core.
Also in this example, what benefit is there to using @Replaces?
The complete example has a Micronaut provided default instance, which needs to be replaced. Think that there's another @Factory
somewhere that provides the same unqualified type, which needs replacement. However, it is not necessary in order to show the defect. I updated the first post anyway to note this.
The singleton question is only tangentially related, and should definitely not affect the qualified bean, because the qualified and unqualified beans should be unrelated and separate.
Indeed, @Primary
does work, but I don't see any reason why @Replaces
does not.
In particular, @Replaces
has named
and qualifier
modifiers, which allows replacing @Named("blah")
(in theory, I did not check), but they seem to be useless to not erase the qualified bean.
from micronaut-core.
In any case, the current behavior does not make sense.
Either:
- The
@Replaces
actually replaces all instances, including differently qualified. This will be a questionable design, but maybe someone can justify it. - It impacts only the very specific type + name/qualifier (if any) and not any other of same type but different qualifier. This seems the most logical and easy to reason about.
Instead, the code erases the differently qualified beans from existence (!) and replaces the one that requires replacement.
from micronaut-core.
Related Issues (20)
- Rejected auth shouldn't lead to "UnexpectedError" being logged with Error level
- Exception implementing HttpResponseProvider thrown from Controller endpoint shouldn't lead to "Unexpected Error" Error log
- Annotation Processor NPE HOT 7
- @Scheduled does not work with expressions in cron value HOT 3
- ./gradlew buildNativeLambda fails due to logback build time init when using dynamodb feature -> Error: Classes that should be initialized at run time got initialized during image building ->
- Introspection Compilation broken for byte[] types that are constructor arguments
- AWSParameterStoreConfigClient Inconsistence with documentation
- Evaluated expressions not working in all annotations they should be (ex: `io.micrometer.core.annotation.Timed`)
- Configuring micronaut.server.netty.compression-level ends in strange StringIndexOutOfBoundsException since version 4.2.0 HOT 1
- @Import doesn't allow injection of generic interfaces
- Handle SerdeException root cause in ConversionErrorException like JsonSyntaxException
- Classes annotated with Lombok @SuperBuilder not introspected correctly HOT 1
- Remove default environment HOT 8
- Consider avoid the scan βfile:config/" by default
- KotlinUtils causing nativeBuild to fail
- Micronaut is decoding the β+β in the path variable passed to the controller code HOT 2
- `IllegalStateException` on controller consuming multipart when error is caught by error handler
- Merge DefaultBeanContext and DefaultApplicationContext
- Always require @ReflectiveAccess if reflection is needed
- Donβt generate reflective Method lookup for executable methods HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micronaut-core.