简体   繁体   中英

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:

What is the @Controller annotation used for? 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. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.

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?

Tnx

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

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

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

I've come across this: Spring MVC 3.1 without annotations?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM