-
I am new to the FX framework and come from a Spring background. In spring I could create 2 beans of the same type and qualify them. EX: @Configuration
class MyConfig {
@Bean
@Qualifier("name1")
public String providerFunction1() {
return "foo"
}
@Bean
@Qualifier("name2")
public String providerFunction2() {
return "bar"
}
@Bean
public Object doSomethingWithThe2Beans(@Qualifier("name1") String bean1, @Qualifier("name2") String bean2) {
...
...
}
} How would I go about doing something similar with fx? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello! You can indeed do that in Fx too. There are a couple ways to produce and consumed named values. Producing named valuesResult objectsResult objects are structs that embed fx.Out respectively. For example, to produce named values, you would write constructors in the following form, type ReadOnlyResult struct {
Conn *db.Conn `name:"ro"`
}
func connectReadOnly() ReadOnlyResult {
// ...
}
type ReadWriteResult struct {
Conn *db.Conn `name:"rw"`
}
func connectReadWrite() ReadOnlyResult {
// ...
} And then var Module = fx.Provide(connectReadOnly, connectReadWrite) fx.AnnotateAlternatively, you can name the results of existing constructors with fx.Annotate. Given the following constructors, func connectReadOnly() *db.Conn { ... }
func connectReadWrite() *db.Conn { ... } You can use fx.Annotate with them to achieve the same as above like so: var Module = fx.Provide(
// Annotates the first result of connectReadOnly to have the given name.
fx.Annotate(connectReadOnly, fx.ResultTags(`name:"ro"`)),
fx.Annotate(connectReadWrite, fx.ResultTags(`name:"rw"`)),
) This is more concise, and we recommend fx.Annotate over fx.Out for most use cases. You can read more about fx.Annotate here: https://pkg.go.dev/go.uber.org/fx#Annotate Consuming named valuesParameter objectsParameter objects are the parameter analog of result objects. type repositoryParams struct {
fx.In
ROConn *db.Conn `name:"ro"`
RWConn *db.Conn `name:"rw"`
}
func newRepository(p repositoryParams) *Repository {
// ...
}
var Module = fx.Provide(newRepository) fx.AnnotateSimilarly to producing values, you can use fx.Annotate to consume named values. func newRepository(roConn, rwConn *db.Conn) *Repository {
// ...
}
var Module = fx.Provide(
fx.Annotate(
newRepository,
// Annotate the first parameter with `name:"ro"`, and the second with `name:"rw"`.
fx.ParamTags(`name:"ro"`, `name:"rw"`),
),
) This is equivalent to the parameter object based variant above. |
Beta Was this translation helpful? Give feedback.
Hello! You can indeed do that in Fx too.
In Fx, we call this "named values".
There are a couple ways to produce and consumed named values.
Producing named values
Result objects
Result objects are structs that embed fx.Out respectively.
You tag fields of such a struct with
name:".."
to have it produce a named value.For example, to produce named values, you would write constructors in the following form,
And then
fx.Provide
them as usual: