Java 如何为需要 MyClass.class 参数的工厂方法注入 Spring Bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18446230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:41:22  来源:igfitidea点击:

How to inject Spring Bean for factory method requiring MyClass.class parameter

javaspringclasspreferencesfactory

提问by Austin Haws

I'm trying to inject a java.util.prefs.Preferences bean in to my master controller. The controller looks like:

我正在尝试将 java.util.prefs.Preferences bean 注入到我的主控制器中。控制器看起来像:

@Controller
class MyController {
    @Autowired
    private Preferences preferences;
}

The application-context.xml file creates the bean for java.util.prefs.Preferences. It uses a factory method so I have the following entry for creating the bean:

application-context.xml 文件为 java.util.prefs.Preferences 创建 bean。它使用工厂方法,所以我有以下用于创建 bean 的条目:

<bean id="preferences" class="java.util.prefs.Preferences" factory-method="userNodeForPackage" />

Preferences.userNodeForPackage(param) takes for a parameter the class related to the Preference. In this case Spring needs to create the bean by performing the call:

Preferences.userNodeForPackage(param) 将与 Preference 相关的类作为参数。在这种情况下,Spring 需要通过执行调用来创建 bean:

Preferences.userNodeForPackage(MyController.class);

How can a class be passed in to a spring bean instantiated with a factory method? Thanks

如何将类传递给使用工厂方法实例化的 spring bean?谢谢

Environment information:

环境信息:

Java 7
Spring 3.1

采纳答案by Sotirios Delimanolis

You can specify the constructor-argelement

您可以指定constructor-arg元素

<bean id="preferences" class="java.util.prefs.Preferences" factory-method="userNodeForPackage">
    <constructor-arg type="java.lang.Class" value="com.path.MyController" />
</bean>

This is explained in the official documentation here, section 5.4.1.

这在官方文档here,第5.4.1节中进行了解释。

Arguments to the static factory method are supplied via elements, exactly the same as if a constructor had actually been used. The type of the class being returned by the factory method does not have to be of the same type as the class that contains the static factory method, although in this example it is. An instance (non-static) factory method would be used in an essentially identical fashion (aside from the use of the factory-bean attribute instead of the class attribute), so details will not be discussed here.

静态工厂方法的参数是通过元素提供的,就像实际使用了构造函数一样。工厂方法返回的类的类型不必与包含静态工厂方法的类的类型相同,尽管在本示例中是。实例(非静态)工厂方法将以基本相同的方式使用(除了使用 factory-bean 属性而不是 class 属性),因此这里不会讨论细节。

回答by Prabhakaran Ramaswamy

 public class Preferences
 {  
     SomeBean someBean;

     public void setSomeBean(SomeBean someBean){
            this.someBean = someBean;
     }  

     public static Preferences createSampleBeanWithIntValue(SomeBean someBean)
     {
         Preferences preferences= new Preferences();
         preferences.setSomeBean(someBean);
         return preferences;
     }
}

  <bean id="someBean" class="java.util.prefs.SomeBean"/>

 <bean id="preferences" class="java.util.prefs.Preferences" factory-method="userNodeForPackage" > 

    <constructor-arg ref="someBean "/>       
 </bean>

Please see the reference

请看参考

http://www.skorks.com/2008/10/are-you-using-the-full-power-of-spring-when-injecting-your-dependencies/

http://www.skorks.com/2008/10/are-you-using-the-full-power-of-spring-when-injecting-your-dependencies/

回答by Ankit

Can you try making "preferences" a property of "MyController". Something like

您可以尝试将“首选项”设为“MyController”的属性吗?就像是

<bean id="MyController" class="com.your.package.MyController">
    <property name="preferences" ref="preferences" />
</bean>

and then have the getter and setter method for preferences in MyController.

然后在 MyController 中为首选项设置 getter 和 setter 方法。

I think this should work.

我认为这应该有效。

回答by Nandkumar Tekale

Well I don't know the xml based configuration way but I can tell you how you can instantiate it via Configurationclass.

好吧,我不知道基于 xml 的配置方式,但我可以告诉您如何通过Configuration类实例化它。

@Configuration
public class Config {
    @Bean(name="preferences")
    public java.util.prefs.Preferences preferences() {
        // init
        return java.util.prefs.Preferences.userNodeForPackage(YourExpectedClass.class);
    }
}

P.S. :

附注:

You will need to add your configuration class/package for scanning either in web.xml if you are using complete annotation based approach [contextClass=org.springframework.web.context.support.AnnotationConfigWebApplicationContext]or in your config file as below :

如果您使用完整的基于注释的方法[contextClass=org.springframework.web.context.support.AnnotationConfigWebApplicationContext]或在您的配置文件中,您将需要在 web.xml 中添加用于扫描的配置类/包,如下所示:

<context:component-scan base-package="com.comp.prod.conf" />

回答by Achilles Ram Nakirekanti

first create the bean of 'Preferences' class either using xml file or using annotation.
then you can use this <context:annotation-config>if you created bean using xml configuration to activate the @Autowired annotation functionality
(or )

首先使用 xml 文件或使用注释创建“首选项”类的 bean。
那么<context:annotation-config>如果您使用 xml 配置创建 bean 来激活 @Autowired 注释功能
(或), 则可以使用它

<context:component-scan base-package="com.estudo.controller" />
if you created bean using annotations.
Note : define the above tags in the spring servlet xml file

<context:component-scan base-package="com.estudo.controller" />
如果您使用注释创建了 bean。
注意:在spring servlet xml文件中定义以上标签

回答by Nu?ito de la Calzada

Spring framework provides facility to inject bean using factory method. To do so, we can use two attributes of bean element.

Spring 框架提供了使用工厂方法注入 bean 的工具。为此,我们可以使用 bean 元素的两个属性。

factory-method: represents the factory method that will be invoked to inject the bean. factory-bean: represents the reference of the bean by which factory method will be invoked. It is used if factory method is non-static. A method that returns instance of a class is called factory method.

factory-method:表示将被调用以注入 bean 的工厂方法。factory-bean:表示将调用工厂方法的 bean 的引用。如果工厂方法是非静态的,则使用它。返回类实例的方法称为工厂方法。

public class A {  
public static A getA(){//factory method  
    return new A();  
}  
}