[英]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.