简体   繁体   English

使用Spring @Controller构造型保持方法可见性的最佳实践

[英]Best practice to keep method visibility with Spring @Controller stereotype

My project at current job use private access modifier for methods of MVC controllers: 我当前作业的项目使用私有访问修饰符来处理MVC控制器的方法:

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    private ModelAndView helloWorld() {

I have integrated PMD and his report has a lot of: 我已经集成了PMD,他的报告有很多:

/src/main/java/com/web/controller/SignalController.java:91: Avoid unused private
                                            methods such as 'handleNewRequest()'.

So instead of disabling useful PMD rule I think to change controller's methods visibility to public . 因此,我认为将控制器的方法可见性更改为公共,而不是禁用有用的PMD规则。

Are there any reasons to keep controller's methods private ? 是否有任何理由将控制器的方法保密

You're shooting yourself in the foot by making it private: 你将自己私有化,从而使自己陷入困境:

  1. it's seen as unused by PMD and the IDEs (and, transitively, all the other private methods it calls, too). 它被PMD和IDE视为未使用(并且,传统上,它也调用了所有其他私有方法)。 So you or a collegue might mistakenly remove private methods that are actually used. 因此,您或同事可能会错误地删除实际使用的私有方法。
  2. It makes it much harder to unit -test them. 这使得对它们进行单元测试变得更加困难。
  3. It's unconventional, making your code look weird to experienced Spring developers. 这是非常规的,使您的代码看起来对经验丰富的Spring开发人员来说很奇怪。
  4. They're logically public, since they're called by code outside of the class and package. 它们在逻辑上是公开的,因为它们是由类和包之外的代码调用的。

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

相关问题 在Spring Controller中重定向的最佳实践 - Best Practice of redirecting in Spring Controller 在Spring应用程序中使用RestController和Controller的最佳实践 - Best practice to have a RestController and a Controller in Spring Application 最佳实践Spring 4 Controller MVC-服务发布 - Best Practice Spring 4 Controller MVC - Service releation Spring REST方法类型最佳实践 - Spring REST best practice for method types Spring MVC最佳实践处理控制器中不可恢复的异常 - Spring MVC Best Practice Handling Unrecoverable Exceptions In Controller 从 Spring 控制器方法发送 Json 响应的最佳实践 - Best practice to send Json response from Spring controller methods 将对象发送到Spring MVC控制器的最佳方法 - Best method to sending an object to a Spring MVC controller 在侦听器的handle方法中是否有最佳方法来调用控制器类的方法? - Is there a best practice way for calling methods of controller class in handle method of a listener? 使用Spring Controller构造型声明控制器与作为AbstractController的子类之间有什么区别? - What's the difference between declaring a controller with the Spring Controller stereotype versus as a subclass of the AbstractController? Java线程可见性 - 没有显式同步的可见性的最佳实践 - Java thread visibility - best practice for visibility without explicit synchronization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM