Giter Club home page Giter Club logo

Comments (10)

dtitov avatar dtitov commented on June 15, 2024

Please, provide more detailed description. And also steps to reproduce the issue (code snippet).

from di.

junneyang avatar junneyang commented on June 15, 2024

Please, provide more detailed description. And also steps to reproduce the issue (code snippet).

  1. 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()
	}
}
  1. 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.

dtitov avatar dtitov commented on June 15, 2024

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.

junneyang avatar junneyang commented on June 15, 2024

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()?

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.

dtitov avatar dtitov commented on June 15, 2024

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.

junneyang avatar junneyang commented on June 15, 2024

thanks very much,
also looking forward to the ordered initialization with PostConstruct feature ~

from di.

github-actions avatar github-actions commented on June 15, 2024

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.

junneyang avatar junneyang commented on June 15, 2024

thanks very much, also looking forward to the ordered initialization with PostConstruct feature ~

how about this feature @dtitov

from di.

dtitov avatar dtitov commented on June 15, 2024

Sorry, but currently I have no time for this. If you like - feel free to provide a pull request.

from di.

github-actions avatar github-actions commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.