Women in Technology

Hear us Roar



Article:
  Using Dependency Injection in Java EE 5.0
Subject:   One class != one object
Date:   2007-05-01 10:06:33
From:   docbillnet
Come on. The whole flaw with annotations is they assume each Object only has a single use. Presumably the reason why they are variables, and not constants in that they can take on different values.


For example, consider if I had the following class:


class DisplayMessage {
Message message;
Message setMessage(Message message) {
this.message = message;
}
Message getMessage() { return message; }
void displayMessage() {
... something complicated ...
}
}


Now perhaps I have many other classes that need DisplayMessage objects injected. Some with different Message values?


In Spring this is pretty easy. I just assign one resource name for each message object I will need, in inject the appropriate message.


For example:
<bean id="displayMessage1" class="Message">
<property name="message" ref="message1"/>
</bean>
<bean id="displayMessage2" class="Message">
<property name="message" ref="message2"/>
</bean>
<bean id="displayMessage3" class="Message">
<property name="message" ref="message3"/>
</bean>


However, with annotations the message is contained within the class itself as an annotation. If you want to actually have different object which only vary by what value is ejected into them, you actually have to make separate classes. How stupid is that?


A commonly used class might be replicated hundreds of times, just so different values can be injected.


Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • One class != one object
    2007-05-01 10:12:33  docbillnet [View]

    Opps. My example should have been:

    <bean id="displayMessage1" class="DisplayMessage">
    <property name="message" ref="message1"/>
    </bean>
    <bean id="displayMessage2" class="DisplayMessage">
    <property name="message" ref="message2"/>
    </bean>
    <bean id="displayMessage3" class="DisplayMessage">
    <property name="message" ref="message3"/>
    </bean>

    Just imagine how cluttered your project files will look with hundreds of duplicated classes.