本文介绍了从第二个类访问后,两个类中的自动装配的Bean不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

在两个类中自动装配的bean,我在一个类中填充该bean,然后在第二个类中检查该bean,但未填充.而当我在填充bean的类中添加getter并调用getter时,它将返回填充的bean.为什么我不能直接在另一个类中访问该bean,因为它在春季上下文中充当单例对象,所以不应该填充它吗?

A bean Autowired in two classes, I populate the bean in one class, then I check the bean in the second class, it is not populated. Whereas, when I add a getter in the class where I populated the bean and call the getter, it returns the bean populated. Why can't I access that bean directly in the other class, shouldn't it be populated since it's acting as a singleton in spring context?

JUnit4测试类从xml文件加载spring上下文:

A JUnit4 test class loading the spring context from an xml file:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/test-main-context.xml" })
public class MyTests {

    @Autowired
    MyObject myObject;

    @Autowired
    MyUtils myUtils;

    @Test
    public void testingContext() {
        myUtils.setMyObjectFromDB();
        System.out.println(myUtils.getMyObject().getId());
        System.out.println(myObject.getId());
    }
}

MyUtils:

public class MyUtils {

    @Autowired MyObject myObject;

    public void setMyObjectFromDB() {
        MyObject myDBObject = new MyObject();
        //
        // getting myObjectFromDB;
        //
        myObject = myDBObject;
    }

    public MyObject getMyObject() {
        return myObject;
    }
}

在测试类中,myUtils.getMyObject().getId()返回正确的id,但第二行myObject.getId()返回null.

In the test class, myUtils.getMyObject().getId() returns a correct id but the second line myObject.getId() it returns null.

当直接在测试类中访问MyObject时,为什么MyObject在MyUtils类中设置并且在@Autowired两个类中都没有更新?

Why is MyObject which is set in MyUtils class and is @Autowired in both classes is not being updated when I access it directly in the test class.

推荐答案

setMyObjectFromDB方法中重新分配myObject = myDBObject;时,您将创建一个新对象并将其Referene保存在不同的myObject变量中从Autowired创建的版本中删除.

When you're reassigning myObject = myDBObject; in setMyObjectFromDB method, you're creating a new object and saving it's referene in myObject variable which is different from the one created with Autowired.

使用Autowired时,它将在变量中分配创建的bean的引用.但是,如果您重新分配该变量,它将指向新对象.

When you use Autowired, then it will assign the created bean's reference in the variable. But if you reassign that variable, it will point to the new object.

如果确实需要使用Autowired初始化的方式更新myObject的所有变量,最好创建一个存储myObject变量的容器类.

If you really need to update all the variables of myObject with initialized with Autowired, it is better to create a container class that stores myObject variable.

public class MyObjectContainer {
     @Autowired
     MyObject myObject;

     // Getters & Setters
}

在所有要自动布线myObject的类中,请使用MyObjectContainer类的对象.而当您想更新myObject值时,只需使用它的设置器在myObjectContainer对象中对其进行更新.因此,您的MyUtils就像:

In all the classes, where you're autowiring myObject, use an object of MyObjectContainer class instead. And when you want to update myObject value, just update it in myObjectContainer object with it's setter. So your MyUtils would be like:

public class MyUtils {

    @Autowired MyObjectContainer myObjectContainer;

    public void setMyObjectFromDB() {
        MyObject myDBObject = new MyObject();
        //
        // getting myObjectFromDB;
        //
        myObjectContainer.setMyObject(myDBObject);
    }

    public MyObjectContainer getMyObjectContainer() {
        return myObjectContainer;
    }
}

这篇关于从第二个类访问后,两个类中的自动装配的Bean不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:24