简体   繁体   English

在Spring 3中通过注释注入与通过XML文件注入。与继承相关的限制

[英]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 . 我有以下抽象类,带有一个名为portletBaseViewName的属性,对于每个扩展AbstractController具体Controller来说,该属性都是不同的。

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: 我知道可以用XML声明注入,这样做是:

<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. 非常感谢。

Do you actually need @Value here? 您实际上是否需要@Value

@Value is useful when it contains some expressions evaluated by Spring at runtime. @Value包含一些在运行时由Spring评估的表达式时很有用。 Otherwise you can replace it with explicit initialization (and keeping the setter method allows you to override this values from XML configuration): 否则,您可以将其替换为显式初始化(​​并且保留setter方法允许您从XML配置中覆盖此值):

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";
    }
}

As shown by @axtavt in previous answer, you don't need any annotation in AbstractController . 如上一个答案中的@axtavt所示,您在AbstractController不需要任何注释。 In addition, you don't need to define abstractController in XML. 另外,您不需要在XML中定义abstractController。 In case, you want to configure common properties in all of its subclasses, you can put them in abstract bean (without any class name): 如果要在其所有子类中配置公共属性,可以将它们放在抽象Bean中(没有任何类名):

<!-- define common properties in abstract bean-->
<bean id="controllerTemplate" abstract="true" />



<bean id="reservationOfBooksViewController" class="es.example.portalweb.portlets.reservationofbooks.controller.ReservationOfBooksViewController" parent="controllerTemplate">
<property name="portletBaseViewName" value="reservationOfBooks" />

In subclasses, @Value annotation is not required and you can use 'getPortletBaseViewName()' method to get view name. 在子类中,@Value注释不是必需的,您可以使用'getPortletBaseViewName()'方法获取视图名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM