简体   繁体   English

强制超类包含无参数构造函数

[英]Force superclasses to include a no-argument constructor

Regarding Spring Controllers; 关于弹簧控制器; when a Constructor is included, a no-arg constructor must be included as well otherwise a 'NoSuchBeanDefinitionException' occurs. 当包含构造函数时,还必须包含无参数构造函数,否则会发生'NoSuchBeanDefinitionException'。 This message is very vague and caused me some headache. 此消息非常模糊,使我有些头疼。

We currently have an abstract controller class which most of our controllers extend from. 当前,我们有一个抽象的控制器类,我们的大多数控制器都从该类扩展而来。 It appeared that when my controller extended this class (Which has a 1 arg constructor), suddenly I received the 'NoSuchBeanDefinitionException'. 看来,当我的控制器扩展此类(其中有1个arg构造函数)时,突然我收到了'NoSuchBeanDefinitionException'。

I'd like to help future developers (or even myself in the future) by forcing anyone extending the abstract controller to include a no-arg constructor. 我想通过强迫任何将抽象控制器扩展为包含无参数构造函数的人来帮助未来的开发人员(甚至将来自己)。 Is this possible? 这可能吗?

Edit, adding code for clarity 编辑,添加代码以使内容更清晰

Here's the class most of the controllers extend from, it includes a few helper methods: 这是大多数控制器扩展的类,其中包括一些辅助方法:

public class AbstractController
{
     private String sessionName;
     // other attributes / final variables

     public AbstractController(String sessionName)
     {
         this.sessionName = sessionName;
     }

     public boolean isCommandInSession(HttpSession session)
     {
         return session.getAttribute(sessionName) != null;
     }

     // Other helper methods which include use of 'sessionName'
}

Example of one of many controllers, which works fine. 许多控制器之一的示例,效果很好。 Without the default constructor, or with only overriding the 1 arg constructor this controller will fail with 'NoSuchBeanDefinitionException' 如果没有默认构造函数,或者仅覆盖1个arg构造函数,则此控制器将失败,并显示“ NoSuchBeanDefinitionException”

@Controller
@SessionAttributes(SomeController.COMMAND)
@RequestMapping(SomeController.URL)
public class SomeController extends AbstractController
{
    private String command = "someUniqueSessionName";

    public SomeController()
    {
        super(COMMAND);
    }
}

You should refactor as below: 您应该按以下方式重构:

public class AbstractController
{    
     public boolean isCommandInSession(HttpSession session)
     {
         return session.getAttribute(getSessionName()) != null;
     }

     abstract String getSessionName();
}

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

相关问题 无参构造函数调用2参数构造函数 - No-argument constructor calling 2-argument constructor Exception或其子类中的无参数构造函数 - No-argument constructor in Exception or its subclasses 隐式调用超类中的无参数构造函数 - Calling no-argument constructor in superclass implicitly 在编译时强制存在无参数构造函数(Java) - Enforce presence of no-argument constructor at compile time (Java) 为无参数构造函数的类创建动态代理 - Create a dynamic proxy for a class without no-argument constructor Firebase数据库:尽管存在错误,但仍出现“无参数构造函数”错误 - Firebase Database: Getting “No-Argument Constructor” Error Despite Having It Scala类的最终def this()声明没有公共的无参数构造函数 - Scala class final def this() declares no public no-argument constructor Android Firebase数据库异常:未定义无参数构造函数 - Android Firebase Database exception: not define a no-argument constructor 即使子类已经有一个已定义的构造函数,父类是否总是需要一个默认或无参数的构造函数? - Does a parent class always need a default or no-argument constructor even if the child class already has a defined constructor? Java无参数构造函数:抛出不可能的异常,或者有空的catch块? - Java no-argument constructor: Throw impossible exception, or have empty catch block?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM