java - Injection through Annotations vs injection through XML file in Spring 3. Limitations related to inheritance -
i have following abstract class, property called portletbaseviewname
meant different every concrete controller extending abstractcontroller
.
public abstract class abstractcontroller { private string portletbaseviewname; protected string getportletbaseviewname() { return portletbaseviewname; } @required @value("") public void setportletbaseviewname(string portletbaseviewname) { this.portletbaseviewname = portletbaseviewname; } } @controller @requestmapping("view") public class reservationofbooksviewcontroller extends abstractcontroller{}
i know possible declaring injections in xml, doing so:
<bean id="abstractcontroller" class="es.alcampo.portalweb.portlets.common.controller.abstractcontroller" abstract="true"> <property name="portletbaseviewname" value="" /> </bean> <bean id="reservationofbooksviewcontroller" class="es.example.portalweb.portlets.reservationofbooks.controller.reservationofbooksviewcontroller" parent="abstractcontroller"> <property name="portletbaseviewname" value="reservationofbooks" /> </bean> <bean id="myshopviewcontroller" class="es.example.portalweb.portlets.reservationofbooks.controller.myshopviewcontroller" parent="abstractcontroller"> <property name="portletbaseviewname" value="myshop" /> </bean>
do need redefine?:
@controller @requestmapping("view") public class reservationofbooksviewcontroller extends abstractcontroller{ @value("reservationofbooks") public void setportletbaseviewname(string portletbaseviewname) { super.setportletbaseviewname(portletbaseviewname); } }
i don't previous option, elegant option if there reach purpose of injecting 1 value or depending on concrete class through annotations?
i know inheritance , annotations conflict.
thanks lot.
do need @value
here?
@value
useful when contains expressions evaluated spring @ runtime. otherwise can replace explicit initialization (and keeping setter method allows override values xml configuration):
public abstract class abstractcontroller { protected string portletbaseviewname = ""; public void setportletbaseviewname(string portletbaseviewname) { this.portletbaseviewname = portletbaseviewname; } } @controller @requestmapping("view") public class reservationofbooksviewcontroller extends abstractcontroller{ public reservationofbooksviewcontroller() { this.portletbaseviewname = "reservationofbooks"; } }
Comments
Post a Comment