Injection through Annotations vs injection through XML file in Spring 3. Limitations related to inheritance

I have the following abstract class, with a property called portletBaseViewName which is meant to be different for 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 that is possible declaring the injections in a 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 I need to redefine?:

@Controller
@RequestMapping("VIEW")
public class ReservationOfBooksViewController extends AbstractController{
    @Value("reservationOfBooks")
    public void setPortletBaseViewName(String portletBaseViewName) {
        super.setPortletBaseViewName(portletBaseViewName);
    }
}

I don't like the previous option, which would be the most elegant option if there is to reach the purpose of injecting one value or another depending on the concrete class through annotations?

I know inheritance and annotations sometimes conflict.

Thanks a lot.

Answers


Do you actually need @Value here?

@Value is useful when it contains some expressions evaluated by Spring at runtime. Otherwise you can replace it with explicit initialization (and keeping the setter method allows you to override this values from 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";
    }
}

Need Your Help

WPF C# application deployment - Nothing happens when running the exe

c# wpf debugging deployment

I've copied over the content of the Debug folder on a Windows 7 VM and a Windows Vista laptop. Both had Dot Net 4.5 installed. All assemblies are set to copy local and AFAIK I don't reference anyth...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.