Java:更改堆上的对象引用?

发布于 2024-09-15 18:26:30 字数 372 浏览 9 评论 0原文

想知道是否有办法更改其他对象正在引用的堆上的对象。

我特别想做的是管理我的瞬态配置。我正在做的是从 JAXB 或 JPA 加载“绑定”配置。我有一个管理器,它维护一些线程来检查这些配置存储是否发生变化。如果这样做,我希望再次从存储中加载配置(创建配置的新实例)并用堆上的新配置实例替换“陈旧”配置实例,因此引用配置数据的任何对象都将获得最新的。

我知道我可能会遇到必须处理分层对象引用的噩梦 - 但我只是想了解各种潜在的方法,然后再决定简单地记录不创建本地引用并始终从配置管理器调用如果您期待最新的 =)

有什么想法如何做到这一点吗?我对 AOP 不太熟悉...但据我所知...我认为这可能提供实现这一目标的途径。

当然,欢迎任何其他想法=)

史蒂夫

Wondering if there is a way to change the object on the heap that other objects are referencing.

What I am specifically trying to do is manage my transient configuration. What I am doing is loading "bound" configuration from JAXB or JPA. I have a manager which maintains some threads to check if those config stores change. If they do, I wish to load the configuration from the store again (creates a new instance of the config) and REPLACE the "stale" configuration instance with the new one on the heap, so any objects referencing the configuration data will get the latest.

I understand I'll likely be running into a nightmare with having to deal with the hierarchical object references - but I simply want to learn about the various potential approaches before I decide to simply document not to create local reference and always call from the config manager if you expect the latest =)

Any ideas how to do this? I'm not too familiar with AOP...but from what I know about it...I am thinking this might provide an avenue to achieve this.

Any other ideas are welcome, of course =)

Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2024-09-22 18:26:30

我对 JAXB 或 JPA 一无所知,但这就是我要做的。为您的各种对象提供对配置包装器的引用。然后,您可以更新配置(以同步方式),而无需更改这些引用:

interface Config { String getSomeProperty(); }

class ConcreteConfig implements Config{
    public String getSomeProperty() {
        return "some value";
    }
}

class ConfigWrapper implements Config {
    private Config backing;
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void setBacking(ConfigBacking backing) {
        try {
            lock.writeLock().lock();
            this.backing = backing;
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public String getSomeProperty() {
        try {
            lock.readLock().lock();
            return backing.getSomeProperty();
        } finally {
            lock.readLock().unlock();
        }
    }
}

然后您只需分发 ConfigWrapper 的实例,并且可以在需要时自由地重新分配支持对象。

I don't know anything about JAXB or JPA, but here's what I'd do. Give your various objects a reference to a wrapper for the config. Then you can update the config (in a synchronized manner) without needing to change those references:

interface Config { String getSomeProperty(); }

class ConcreteConfig implements Config{
    public String getSomeProperty() {
        return "some value";
    }
}

class ConfigWrapper implements Config {
    private Config backing;
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void setBacking(ConfigBacking backing) {
        try {
            lock.writeLock().lock();
            this.backing = backing;
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public String getSomeProperty() {
        try {
            lock.readLock().lock();
            return backing.getSomeProperty();
        } finally {
            lock.readLock().unlock();
        }
    }
}

Then you would only distribute the instance of ConfigWrapper, and can freely reassign the backing object whenever you want.

大海や 2024-09-22 18:26:30

您可以查看 Spring 框架。如果我没记错的话,它支持类似的东西(也许 this< /a>?)。

You can check out the Spring framework. If my memory serves, it supports something like that (maybe this?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文