Comments (10)
Please, provide more detailed description. And also steps to reproduce the issue (code snippet).
from di.
Please, provide more detailed description. And also steps to reproduce the issue (code snippet).
- code example:
package main
import (
"fmt"
"reflect"
"github.com/goioc/di"
)
type A1 struct {
Name string
}
func (a1 *A1) PostConstruct() error {
//time.Sleep(2 * time.Second)
a1.Name = "a1"
return nil
}
type A2 struct {
A1 *A1 `di.inject:""`
Message string
}
func (a2 *A2) PostConstruct() error {
a2.Message = "hello, " + a2.A1.Name
fmt.Printf("a2.A1: %v, a2.Message: %v \n", a2.A1, a2.Message)
return nil
}
func main() {
for i := 0; i < 20; i++ {
di.RegisterBean("A1", reflect.TypeOf((*A1)(nil)))
di.RegisterBean("A2", reflect.TypeOf((*A2)(nil)))
di.InitializeContainer()
di.Close()
}
}
- test result:
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{a1}, a2.Message: hello, a1
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{a1}, a2.Message: hello, a1
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{a1}, a2.Message: hello, a1
a2.A1: &{a1}, a2.Message: hello, a1
a2.A1: &{a1}, a2.Message: hello, a1
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
a2.A1: &{}, a2.Message: hello,
from di.
OK, I see now, thanks. The reason is that there's no defined order for calling PostConstruct()
, it's indeed random. When the PostConstruct()
is called for A2
- it doesn't necessarily mean that it was already called for A1
.
I'm not sure if it's something to be fixed, to be honest, because most likely your real scenario can be implemented in a better way, i.e. by defining whatever you like to inject as a bean.
But, out of curiosity, how do you see the solution for this? Would you like to have a feature that would allow ordered initialization with PostConstruct()
?
from di.
OK, I see now, thanks. The reason is that there's no defined order for calling
PostConstruct()
, it's indeed random. When thePostConstruct()
is called forA2
- it doesn't necessarily mean that it was already called forA1
.I'm not sure if it's something to be fixed, to be honest, because most likely your real scenario can be implemented in a better way, i.e. by defining whatever you like to inject as a bean.
But, out of curiosity, how do you see the solution for this? Would you like to have a feature that would allow ordered initialization with
PostConstruct()
?
i would better to ask you a better implement on this scenario,
then, allow ordered initialization with PostConstruct()
is much better (i also test spring, it's postconstruct is ok)
thanks very much
from di.
How about this?
type A1 struct {
Name *string `di.inject:"A1Name"`
}
type A2 struct {
A1 *A1 `di.inject:""`
Message string
}
func (a2 *A2) PostConstruct() error {
a2.Message = "hello, " + *a2.A1.Name
return nil
}
func (suite *TestSuite) Test() {
for i := 0; i < 20; i++ {
a1Name := "a1"
RegisterBeanInstance("A1Name", &a1Name)
RegisterBean("A1", reflect.TypeOf((*A1)(nil)))
RegisterBean("A2", reflect.TypeOf((*A2)(nil)))
InitializeContainer()
a2 := GetInstance("A2").(*A2)
fmt.Printf("a2.A1: %v, a2.Message: %v \n", a2.A1, a2.Message)
Close()
}
}
If you define the name for the A1
as a string
-bean, then the problem is solved, because now it's an explicit dependency which is guaranteed to be already set by the time you call PostConstruct()
for A2
.
from di.
thanks very much,
also looking forward to the ordered initialization with PostConstruct
feature ~
from di.
This issue is stale because it has been open 20 days with no activity. Remove stale label or comment or this will be closed in 5 days.
from di.
thanks very much, also looking forward to the
ordered initialization with PostConstruct
feature ~
how about this feature @dtitov
from di.
Sorry, but currently I have no time for this. If you like - feel free to provide a pull request.
from di.
This issue is stale because it has been open 20 days with no activity. Remove stale label or comment or this will be closed in 5 days.
from di.
Related Issues (15)
- [Bug report]
- [Feature request] HOT 2
- Improve beans initialization HOT 2
- [Feature request] register&inject bean with named group HOT 6
- [Feature request] when the Prototype bean destroy? HOT 3
- Fail to inject dependencies HOT 5
- Question regarding the implmentation of PostConstruct HOT 1
- Been Injection Not Working HOT 2
- [Feature request]Circular dependency HOT 1
- [Bug report] Can't get another bean instance from bean factory, despite of the docs HOT 5
- [Feature request] Add examples about how to use this library HOT 1
- replace ioutil.ReadAll with io.ReadAll in README HOT 1
- [Feature request] Autowiring beans into slice or map depending on the interface HOT 6
- [Feature request] Bean lifecycle 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 di.