【问题标题】:Usage of @Componenscan in Springboot Spring MVC ApplicationSpringboot Spring MVC Application中@Componenscan的使用
【发布时间】:2016-09-15 12:24:05
【问题描述】:

我的问题是我不知道如何为我的 Controller 包指定路径以在主类中的 @componenscan 注释中搜索其中的 requestmapping 注释。

项目结构:

我的 DemoApplication 类:

package com.personalitymeet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Configuration
@EnableAutoConfiguration
@ComponentScan()
@Controller
public class DemoApplication {

@ResponseBody
@RequestMapping("/")
String entry(){
    return "bla";
}
public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}
}

用户控制器.java:

package com.personalitymeet.web;

import com.personalitymeet.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by mv on 15.09.2016.
 */

@Controller
public class UserController {

    @RequestMapping("/user")
    public String user(Model model){
        User user = new User();
        user.setFirstname("Misi");
        user.setLastname("Varga");
        model.addAttribute("user",user);
        return "userview";
    }

}

那么,我的问题是,我如何告诉 springboots 它应该在 Usercontroller 类中搜索 @Requestmapping 注解?

【问题讨论】:

  • @EnableWebMvc 解决问题
  • 添加你的 gradle 文件。

标签: java spring spring-mvc intellij-idea spring-boot


【解决方案1】:

您可以在配置文件中指定组件扫描以扫描文件夹com.personalitymeet,它会自动选择所有具有相关注释的类。

以下是初始化应用程序所需的基本 3 个注释。

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.personalitymeet")

【讨论】:

  • 使用 Spirng Boot,您不需要@EnableWebMvc
【解决方案2】:

您的问题不在于@ComponentScan,它将在com.personalitymeet 和所有子包中进行搜索。就是由于某种原因,您没有激活 MVC 基础架构。

您应该将spring-boot-starter-web 作为build.gradle 文件中的依赖项,并且应该将@EnableWebMvc 添加到您的配置类中。

【讨论】:

  • 它在我的 gradle 文件中: dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot- starter-web') 运行时('com.h2database:h2') testCompile('org.springframework.boot:spring-boot-starter-test') }
  • 只添加依赖就足够了... Spring Boot 检测到并自动添加@EnableWebMvc 配置类。如果不是这种情况,则您缺少依赖项。
  • 如果您使用的是 Spring Boot,添加 @EnableWebMvc 几乎肯定是个坏主意。它将关闭 Spring Boot 对 Spring MVC 的所有自动配置。
  • @AndyWilkinson 根据隐含的依赖关系,看起来 Web 启动器已被包含在内,而且还有其他东西在抑制 MVC 自动配置。
【解决方案3】:

你能替换这段代码吗:

@Configuration
@EnableAutoConfiguration
@ComponentScan()
@Controller
public class DemoApplication {
...

使用此代码:

@SpringBootApplication
@Controller
public class DemoApplication {
...

配置您的应用程序以公开管理端点:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready

然后点击此端点以查看应用程序暴露的端点:

http://localhost:[port]/manage/mappings 

另外,我建议为根创建一个单独的控制器,以使您的启动类与应用程序逻辑保持一致。

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 2019-03-11
    • 1970-01-01
    • 2013-10-31
    • 2017-06-23
    • 1970-01-01
    • 2021-11-16
    • 2013-04-12
    • 2016-12-13
    相关资源
    最近更新 更多