Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autowired(name: )可以是对象属性? #29

Closed
tianmafly opened this issue Oct 23, 2022 · 16 comments
Closed

Autowired(name: )可以是对象属性? #29

tianmafly opened this issue Oct 23, 2022 · 16 comments

Comments

@tianmafly
Copy link

tianmafly commented Oct 23, 2022

class A
{
public string str = "test";

    [Autowired(name: str)]
    public B b {get; set;}

}
B有多个子类,每个子类指定名字,将B注入时,使用属性str指向其中的子类。
目前会报错,请问可以这样实现吗?
class A
{
public const string str = "test";

    [Autowired(name: str)]
    public B b {get; set;}

}
const就没问题,但是我想从配置文件读取相应的子类名字,启动对应的子类。str的值实际上从配置文件获取,就不能时const。

@yuzd
Copy link
Owner

yuzd commented Oct 23, 2022

是付本地配置文件吗

@yuzd
Copy link
Owner

yuzd commented Oct 23, 2022

@yuzd
Copy link
Owner

yuzd commented Oct 23, 2022

目前好像还不支持Autowired支持SPEL表达式配置获取
我研究下怎么搞

@yuzd
Copy link
Owner

yuzd commented Oct 24, 2022

@tianmafly
Copy link
Author

需要从nuget下新的依赖?
怎么使用?

@tianmafly
Copy link
Author

class A
{
public const string str = "test";

[Autowired(name: str)]
public B b {get; set;}

}
const就没问题,但是我想从配置文件读取相应的子类名字,启动对应的子类。str的值实际上从配置文件获取,就不能时const。

@yuzd
Copy link
Owner

yuzd commented Oct 24, 2022

升级最新版

看这个单测

https://github.com/yuzd/Autofac.Annotation/blob/master/test/issue29/TestAutowiredWithValue.cs#L105-L132

public interface ITestAutowiredWithValueModel
{
string say();
}
[Component("test1")]
public class TestAutowiredWithValueModel1 :ITestAutowiredWithValueModel
{
public string say()
{
return "test1";
}
}
[Component("test2")]
public class TestAutowiredWithValueModel2 :ITestAutowiredWithValueModel
{
public string say()
{
return "test2";
}
}
[Component]
public class TestAutowiredWithValueModel3
{
[Autowired("${ITestAutowiredWithValueModel}")]
public ITestAutowiredWithValueModel TestAutowiredWithValueModel1 { get; set; }

配置文件:

https://github.com/yuzd/Autofac.Annotation/blob/master/test/appsettings.json#L9

"ITestAutowiredWithValueModel": "test2"

@tianmafly
Copy link
Author

配置文件路径和名字不需要指定吗?配置文件名字不是appsetings.json

@yuzd
Copy link
Owner

yuzd commented Oct 24, 2022

默认是appsetings.json
也可以自己指定配置文件
你参考我写的单测

@yuzd yuzd closed this as completed Oct 24, 2022
@tianmafly
Copy link
Author

tianmafly commented Oct 25, 2022

class A
{

[Autowired(“${abc}”)]
public IValue 《B》 b {get; set;}
}

IValue能结合Autowired吗,这样动态加载类型

@yuzd
Copy link
Owner

yuzd commented Oct 25, 2022

你这么用不对吧
Autowired是装载类的
IValue装载数据的

你这样用的方式如果仅仅是为了动态加载类的话,可以直接用Autowired的就行了

@tianmafly
Copy link
Author

动态加载类的话,可以直接用Autowired的就行了
但这个程序启动后,就不可以再改变了吧。
IValue不是可以监听文件有改变能获取到最新的值,我的理解是程序启动后,修改配置文件仍然可以更新。就行nacos一样?

@yuzd
Copy link
Owner

yuzd commented Oct 26, 2022

可以按照下面的方式来每次都是拿一个新的
image

@yuzd
Copy link
Owner

yuzd commented Oct 26, 2022

你可以试下上面那样的使用方式

[Autowired("${test}")]
public ObjectFactory<A38> A38 {get;set;}

先读,然后sleep(10s )
在sleep的时候你去修改下本地配置文件的内容

然后sleep完成后在去拿 check是不是变成了另外的一个实现类

nacos的话之前有做过一个demo
#22

@tianmafly
Copy link
Author

[Autowired("${test}")]
public ObjectFactory a38 {get;set;}
使用时需要先A38 a = a38.GetObject()获取A38对象吗?
再调用a的方法?如果多个方法都需要调用a38的方法,感觉这样不太优雅。
能不能在一个地方A38 a = a38.GetObject()获取A38对象,其他方法中直接a.fun()?

@yuzd
Copy link
Owner

yuzd commented Oct 29, 2022

因为需要通过这个ObjectFactory拿才能每次拿到新的,
不然动态更新了配置 你就拿不到了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants