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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29410179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:13:09  来源:igfitidea点击:

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

javaspringspring-mvccontrollerannotations

提问by AndreaNobili

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? How can you create a controller without an annotation?

@Controller 注释有什么用?如何在没有注释的情况下创建控制器?

So I know that the @Controllerannotation indicates that a particular class serves the role of a controller. The @Controllerannotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMappingannotations.

所以我知道@Controller注释表明一个特定的类充当控制器的角色。@Controller注解作为注解类的构造型,表明它的作用。调度程序扫描此类带注释的类以查找映射方法并检测@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

田纳西州

回答by Saakshi Aggarwal

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

web.xml

网页.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>

回答by SSJVegito

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 文件中使用特殊的配置。

回答by Murilo Tuvani

Just to comment the reasons why someone would like to configure Spring programatically or throuth XML, it is because it takes some to scan all the files looking for the annotations during runtime, so if we disable the scan and configure it manually the application will be available to service requests much faster and that is very important on high demand sceneraios.

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