春季注入绑定到实例 [英] Spring injection bind toInstance

查看:71
本文介绍了春季注入绑定到实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以使用Spring DI将注入的对象绑定到特定实例,类似于Google Guice的

Is there a way to bind an injected object to a specific instance using Spring DI similar to Google Guice's

bind(MyClass.class).toInstance(myclassobject);


推荐答案

如果构造函数或成员变量用<$注释c $ c> @Autowired ,Spring将尝试查找与Object类型匹配的bean。您可以使用 @Qualifier 获得与注释类似的功能,例如:

If the constructor or member variable is annotated with @Autowired, Spring will try to find a bean that matches the type of the Object. You can get similar functionality to the annotation using @Qualifier, for example:

bind(MyClass.class).annotatedWith(Names.named("main")).toInstance(myclassobject);

会在春季出现:

@Autowired @Qualifier("main") private MyClass myClassObject;

<bean name="myClassObject" class="example.MyClassImpl">
    <qualifier value="main"/>
</bean>

请参见 http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-autowired-annotation (更多)。

这篇关于春季注入绑定到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆