如何让一个类使用applicationContext bean? [英] How to have a class use applicationContext beans?

查看:70
本文介绍了如何让一个类使用applicationContext bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个java struts2 webapp,并且希望能够将Bean参数注入到任何所调用的任意类中.但是我发现我只能使用struts动作类来做到这一点...

I'm working on my first java struts2 webapp and want to be able to inject bean parameters into any arbitrary class that is called. But I find that I can only do this with struts action classes...

假设我的applicationContext.xml文件中有这个bean:

Let's say I have this bean in my applicationContext.xml file:

<bean id="BeanTest" class="BeanTest">
    <property name="test" value="someval" />
</bean>

如果我有一个名为BeanTest的struts动作类设置(像这样),并且添加了一个setter(公共void setTest()),那么将设​​置测试参数,并且我可以访问它.

If I have a struts action class setup called BeanTest (like so), and I add a setter (public void setTest()), then the test parameter will be set and I can access it.

import com.opensymphony.xwork2.ActionSupport;

public class BeanTest extends ActionSupport{
    private String test;

    public String execute(){
        String str = getTest(); // returns "someval"
        return "success";
    }

    public void setTest(String test){
        this.test = test;
    }

    public String getTest(){
        return test;
    }
}

但是,假设我像这样将Bean更改为BeanTest2:

However, let's say I change the bean to BeanTest2 like so:

<bean id="BeanTest2" class="BeanTest2">
    <property name="test" value="someval" />
</bean>

我有一个像这样的独立类:

And I have a standalone class like so:

public class BeanTest2{
    private test;

    public void setTest(String test){
        this.test = test;
    }

    public String getTest(){
        return test;
    }
}

如果我在BeanTest中创建BeanTest2的实例,并调用getTest,它将始终返回null.

If I create an instance of BeanTest2 in BeanTest, and call getTest, it always returns null.

import com.opensymphony.xwork2.ActionSupport;

public class BeanTest extends ActionSupport{
    public String execute(){

        BeanTest2 bt = new BeanTest2();
        String str = bt.getTest(); //returns null, but I want "someval"

        return "success";
    }
}

我想做的是在applicationContext中设置一个bean,以便我可以将其指向任意类,并且该类将始终获得我设置的任何bean参数(假设我已经为它们创建了setter).不幸的是,发生的事情是只有struts动作类才能获得这些bean属性.一切都还没有完成.

What I want to do is set up a bean in applicationContext so that I can point it to an arbitrary class and that class will always get whatever bean parameters I set (assuming I've created setters for them). Unfortunately, what happens is that only struts action classes are able to get these bean properties. Everything is not getting set.

这个问题清楚吗?我觉得我缺少关于豆子工作方式的明显认识.

Is this question clear? I feel like I'm missing something obvious about the way beans work.

推荐答案

我认为Spring通常只会对Spring创建的那些类进行依赖注入,而不是您使用new运算符创建的那些类.

I think Spring will normally do dependency injection only for those classes that are created by Spring, not for the ones that you create yourself with new operator.

BeanTest是由Spring创建的,因此它将注入其依赖项,但是BeanTest2不是由Spring创建的,因此Spring对BeanTest2实例一无所知.

BeanTest is created by Spring so it will get its dependencies injected, but BeanTest2 is not created by Spring, thus Spring knows nothing about BeanTest2 instances.

您可以将BeanTest2添加为BeanTest中的字段

You can add BeanTest2 as a field in BeanTest

public class BeanTest {
   private BeanTest2 beanTest2;
   public void setBeanTest2(BeanTest2 b) { this.beanTest2 = b; }
   public BeanTest2 getBeanTest2() { return this.beanTest2; };
}

然后,您可以将beanTest2注入beanTest实例.

Then you can inject beanTest2 to beanTest instance.

<bean id="beanTest2" class="BeanTest2">
    <property name="test" value="someval" />
</bean>

<bean id="beanTest" class="BeanTest">
    <property name="beanTest2" ref="beanTest2" />
</bean>

这样,应该将beanTest2注入到BeanTest实例中.

This way beanTest2 should be injected into BeanTest instance.

这篇关于如何让一个类使用applicationContext bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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