SpringBootApplication注解在SpringBoot中扮演着怎样的角色?

2026-05-27 21:551阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计728个文字,预计阅读时间需要3分钟。

SpringBootApplication注解在SpringBoot中扮演着怎样的角色?

在demo中,我们将启动方法和Controller分别放在一个类中。在实际开发中,启动方法位于一个独立的类中。因此,我们创建一个SpringApplication类作为独立的启动类,并将Controller分离出来。


在demo里面,我们把启动方法和controller放在了一个类里面,在实际开发中,启动方法是放在一个单独的类里面的。

为此我们创建一个SpringApplication类作为单独的启动类,和Controller分离。创建目录如下:

SpringBootApplication注解在SpringBoot中扮演着怎样的角色?

编辑

①,Application.java

package com.springboot.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})
@ComponentScan("com.springboot.controller") //指定Controller所在的包,默认扫描当前包以及当前包的子包
public class Application {

public static void main(String[] args)

②,IndexController.java

package com.springboot.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ConfigurationProperties(prefix = "PEOPLE")
public class IndexContoller {

@RequestMapping("/index")
@ResponseBody
public String index(){
return "Index";
}

/* @Value("${PEOPLE.name}")
private String name;

@Value("${PEOPLE.sex}")
private String sex;*/

private String name;

private String sex;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@RequestMapping("/info")
@ResponseBody
public String getPeopleInfo(){
return name+":"+sex;
}

}

启动访问:​​localhost:8088/dev/info​​

编辑

二,@SpringBootApplication(组合注解)

在Application启动类中,我们用了两个注解,我们可以用一个注解SpringBootApplication替换它们两个

//@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})
//@ComponentScan("com.springboot.controller") //指定Controller所在的包,默认扫描当前包以及当前包的子包
@SpringBootApplication(scanBasePackages = {"com.springboot.controller"},exclude = {RedisAutoConfiguration.class})
public class Application {

public static void main(String[] args)

SpringBootApplication注解默认扫描当前包以及当前包的子包

=========扩展=======

@RestController

这个也是一个组合注解,当一个Controller中所有的功能方法都返回restful内容时,我们就可以将@Controller换成@RestController,这样的好处时不用每个方法都使用@ResponseBody。接下来以修改IndexController为例。

①,IndexController.java

package com.springboot.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ConfigurationProperties(prefix = "PEOPLE")
public class IndexContoller {

@RequestMapping("/index")
public String index(){
return "Index";
}

/* @Value("${PEOPLE.name}")
private String name;

@Value("${PEOPLE.sex}")
private String sex;*/

private String name;

private String sex;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@RequestMapping("/info")
public String getPeopleInfo(){
return name+":"+sex;
}

}

②。启动并访问查看结果,这里我就不截图结果图了,请自行验证。



本文共计728个文字,预计阅读时间需要3分钟。

SpringBootApplication注解在SpringBoot中扮演着怎样的角色?

在demo中,我们将启动方法和Controller分别放在一个类中。在实际开发中,启动方法位于一个独立的类中。因此,我们创建一个SpringApplication类作为独立的启动类,并将Controller分离出来。


在demo里面,我们把启动方法和controller放在了一个类里面,在实际开发中,启动方法是放在一个单独的类里面的。

为此我们创建一个SpringApplication类作为单独的启动类,和Controller分离。创建目录如下:

SpringBootApplication注解在SpringBoot中扮演着怎样的角色?

编辑

①,Application.java

package com.springboot.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})
@ComponentScan("com.springboot.controller") //指定Controller所在的包,默认扫描当前包以及当前包的子包
public class Application {

public static void main(String[] args)

②,IndexController.java

package com.springboot.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ConfigurationProperties(prefix = "PEOPLE")
public class IndexContoller {

@RequestMapping("/index")
@ResponseBody
public String index(){
return "Index";
}

/* @Value("${PEOPLE.name}")
private String name;

@Value("${PEOPLE.sex}")
private String sex;*/

private String name;

private String sex;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@RequestMapping("/info")
@ResponseBody
public String getPeopleInfo(){
return name+":"+sex;
}

}

启动访问:​​localhost:8088/dev/info​​

编辑

二,@SpringBootApplication(组合注解)

在Application启动类中,我们用了两个注解,我们可以用一个注解SpringBootApplication替换它们两个

//@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})
//@ComponentScan("com.springboot.controller") //指定Controller所在的包,默认扫描当前包以及当前包的子包
@SpringBootApplication(scanBasePackages = {"com.springboot.controller"},exclude = {RedisAutoConfiguration.class})
public class Application {

public static void main(String[] args)

SpringBootApplication注解默认扫描当前包以及当前包的子包

=========扩展=======

@RestController

这个也是一个组合注解,当一个Controller中所有的功能方法都返回restful内容时,我们就可以将@Controller换成@RestController,这样的好处时不用每个方法都使用@ResponseBody。接下来以修改IndexController为例。

①,IndexController.java

package com.springboot.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ConfigurationProperties(prefix = "PEOPLE")
public class IndexContoller {

@RequestMapping("/index")
public String index(){
return "Index";
}

/* @Value("${PEOPLE.name}")
private String name;

@Value("${PEOPLE.sex}")
private String sex;*/

private String name;

private String sex;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@RequestMapping("/info")
public String getPeopleInfo(){
return name+":"+sex;
}

}

②。启动并访问查看结果,这里我就不截图结果图了,请自行验证。