将Java Spring注入与“公共静态最终对象"一起使用(适用于Jakarta Unstandard) [英] Using Java Spring injection with `public static final` objects (for Jakarta Unstandard)

查看:54
本文介绍了将Java Spring注入与“公共静态最终对象"一起使用(适用于Jakarta Unstandard)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我知道尝试使用Spring注入静态变量被认为是不好的做法(而且我知道有很多解决方法,例如

Disclaimer: I understand that trying to use Spring to inject static variables is considered bad practice (and I know there are ways around it, e.g. here). So ultimately I plan to redesign, but am curious about possible solutions or workarounds.

我正在使用雅加达的非标准标记库(尤其是 useConstants )轻松地将public static final对象公开给我的JSP页面.我希望这些静态对象从数据库中初始化自身,这意味着我需要注入JDBC模板或数据源.所以我想要类似的东西:

I am using Jakarta's Unstandard tag library (particularly useConstants) to easily expose public static final objects to my JSP pages. I want these static objects to initialize themselves from my database, which means I need to inject a JDBC Template or Data Source. So I want something like:

public class MyGroup {

    // @Autowire or inject somehow?
    private static /*final?*/ NamedParameterJdbcTemplate jdbcTemplate;

    public static final MyGroup GROUP_A = new MyGroup("GROUP_A");
    public static final MyGroup GROUP_B = new MyGroup("GROUP_B");
    public static final MyGroup GROUP_C = new MyGroup("GROUP_C");

    // Instance fields
    private int id;
    private String name;
    private String description;

    /**
     * Construct a group
     */
    public MyGroup() {}

    /**
     * Construct a group using information from the database
     * @param key the key to match
     */
    public MyGroup(String key) {
        // Do DB stuff using injected JDBC template
        this.id = id_from_DB;
        this.name = name_from_DB;
        this.description = desc_from_DB;
    }
}

在我的JSP中,我可以简单地执行${MyGroup.GROUP_A.id},而在Java代码中的其他任何地方也可以执行MyGroup.GROUP_B.getName().

In my JSP, I could simply do ${MyGroup.GROUP_A.id} and anywhere else in the Java code I could just MyGroup.GROUP_B.getName().

因此,问题在于这些组必须为final,以供Jakarta库使用,但我无法通过Spring对其进行静态初始化.有想法吗?

So the problem is that these groups must be final for the Jakarta library to pick them up, but I can't static initialize them via Spring. Thoughts?

推荐答案

spring并不是问题,而是您想要的内容与java所允许的内容之间存在冲突.您不能延迟静态最终属性的分配.加载类时必须设置它.因此,在弹簧可以注入的时候,为时已晚.

This isn't a problem with spring so much as with a conflict between what you want and what java allows. You cannot delay the assignment of a static final property. It has to be set when the class is loaded. Therefore, by the time spring could inject, it is too late.

如果您不必最终决定,则可以打开一些选项.

If you don't have to have it be final, you can open up some options.

另一种可能性是,当拦截该属性的访问并返回所需的值而不是存储的值时,可能会创建一个方面.然后,您可以将所需的值注入到方面.

Another possibility is it might be possible to create an aspect when intercepts the access of the property, and returns the value you want rather than the stored value. You could then inject the desired value into the aspect.

我以前从未专门针对静态属性进行过此操作,但我认为这是可能的.由于java要求内联这些字段,因此无法使用常量字段(绑定到常量字符串对象或原始值的静态最终字段)作为JoinPoint,但是由于您指向的是非String对象,因此我认为使用方面可以工作.

I've never done it before specifically with static properties, but I presume it is possible. It is not possible to use constant fields (static final fields bound to a constant string object or primitive value) as a JoinPoint since java requires those to be inlined, but since you are pointing to a non-String object, I think using an aspect could work.

要确保将Spring注入您的方面,请确保您通过类似以下方式将其告诉spring:

To make sure spring injects into your aspect, make sure you tell spring about it via via something like this:

<bean id="someId" class="com.yourdomain.YourAspect" factory-method="aspectOf"/>

这篇关于将Java Spring注入与“公共静态最终对象"一起使用(适用于Jakarta Unstandard)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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