Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constants and annotation

Tags:

java

This may be a silly question, due to missing some understanding of java but I have this code:

@Stateless
@WebService
public class MLHRequesterBean implements MLHRequesterBeanRemote {

    private final static String sel = "MLHRequesterPU" + (isProduction()? " " : "-dev");

    public static boolean isProduction(){
        try {
           if (Inet4Address.getLocalHost().getHostName().equalsIgnoreCase("ironman")) {
                return true;
            }
        } catch (UnknownHostException ex) {}
        return false;
    }

    @PersistenceContext(unitName=sel)
    ...

Why is sel not considered a constant? We have our test server and a production server and each one should write to a different DB. How can I overcome this issue?

This is the error:

C:\projects\workspace\MLHRequester\MLHRequester-ejb\src\java\mlh\MLHRequesterBean.java:33: attribute value must be constant @PersistenceContext(unitName=sel) 1 error

like image 290
arinte Avatar asked May 28 '26 15:05

arinte


1 Answers

sel is a final static, but its value is evaluated the first time this class is loaded. The @annotations are evaluated at compile time, hence the error.

You are better off doing something like a macro/substitution pre-processing step during build to generate the right value (may be base it off a .properties file).

like image 155
Chii Avatar answered May 30 '26 05:05

Chii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!