简体   繁体   English

如何在不使用注释的情况下创建Spring控制器?

[英]How can I create a Spring controller without the use of the annotations?

I am studying for the Spring Core certification and I have some doubts related this question: 我正在攻读Spring Core认证,我对这个问题有些怀疑:

What is the @Controller annotation used for? @Controller注释用于什么? How can you create a controller without an annotation? 如何在没有注释的情况下创建控制器?

So I know that the @Controller annotation indicates that a particular class serves the role of a controller. 所以我知道@Controller注释表明一个特定的类服务于控制器的角色。 The @Controller annotation acts as a stereotype for the annotated class, indicating its role. @Controller注释充当带注释的类的构造型,指示其角色。 The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations. 调度程序扫描这些带注释的类以查找映射方法,并检测@RequestMapping注释。

So a controller class is something like this: 所以控制器类是这样的:

@Controller
public class AccountController {

    @RequestMapping("/listAccounts")
        public String list(Model model) {...}
    }
}

Ok, this is pretty clear for me but what exactly means create a controller without an annotation? 好的,这对我来说非常清楚,但究竟是什么意思创建一个没有注释的控制器? How can I do it? 我该怎么做? By XML configuration or how? 通过XML配置还是如何?

Tnx TNX

public class AccountController extends MultiActionController {
    public ModelAndView listAccounts() {
        // your code
        return new ModelAndView("listAccounts.bean", "msg", "As you  want")
    }
}

web.xml web.xml中

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.bean</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml 调度员servlet.xml中

<bean name="/listAccounts.bean" class="p1.AccountController"></bean>

I've come across this: Spring MVC 3.1 without annotations? 我遇到过这个问题: 没有注释的Spring MVC 3.1?

It would seem that you can actually create a controller without annotations (I've been working with Spring for little over a year and haven't encountered such a scenario, I don't know why one would want to do this, apart from certification questions of course) and the way to do it is by using a special configuration in the dispatcher-servlet XML file. 看起来你实际上可以创建一个没有注释的控制器(我已经和Spring一起工作了一年多而没有遇到过这种情况,我不知道为什么人们会想要这样做,除了认证当然是问题)以及执行此操作的方法是在dispatcher-servlet XML文件中使用特殊配置。

只是为了评论为什么有人想以编程方式或通过XML来配置Spring的原因,这是因为在运行时需要扫描所有寻找注释的文件,因此如果我们禁用扫描并手动配置它,应用程序将可用更快地服务请求,这对于高需求的场景特征非常重要。

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

相关问题 我只能通过xmls使用没有注释的Spring吗? - Can I use Spring without annotations at all only through xmls? 如何在没有注释的情况下使用 Spring 数据 JDBC? - How to use Spring Data JDBC without annotations? Java - 如何组合验证和AOP注释并在Spring Controller中使用它? - Java - How to combine Validation and AOP annotations and use it in Spring Controller? 如何使用注释对Spring Controller进行单元测试? - How do I unit test Spring Controller using annotations? 我可以在一种控制器方法中使用2个模型属性注释吗? - Can I Use 2 Model Attributes Annotations in One Controller Method? 如何在不使用xml或注释的情况下使Spring识别bean? - How can I make Spring identify beans without using xml or annotations? 如何在不使用if else的情况下实现Factory模式或在春季使用注释切换大小写 - How can i implement Factory pattern without using if else or switch case in spring using annotations 如何在 Spring 引导库(无主类)中使用注释? - How to use Annotations in a Spring Boot Library (without Main Class)? 如何在没有Spring Boot的情况下创建Spring Sleuth应用程序 - How can I create a Spring sleuth application without Spring boot 如何创建用于 spring 安全表达式语言注释的自定义方法 - How to create custom methods for use in spring security expression language annotations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM