16

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

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

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

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

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

Preferences.userNodeForPackage(MyController.class);

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

环境信息:

Java 7
Spring 3.1
4

6 回答 6

23

您可以指定constructor-arg元素

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

此处的官方文档第 5.4.1 节对此进行了说明。

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

于 2013-08-26T14:18:12.537 回答
3

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

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

PS:

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

<context:component-scan base-package="com.comp.prod.conf" />
于 2013-08-26T14:25:47.687 回答
2
 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>

请看参考

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

于 2013-08-26T14:19:29.213 回答
0

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

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

public class A {  
public static A getA(){//factory method  
    return new A();  
}  
}  
于 2017-03-25T05:10:37.930 回答
0

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

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

于 2016-06-08T12:10:11.003 回答
-1

您可以尝试将“首选项”作为“MyController”的属性。就像是

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

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

我认为这应该有效。

于 2013-08-26T14:19:47.563 回答