SpringBoot学习笔记

本文介绍了SpringBoot的基本使用方法,包括创建pom文件、编写启动类、控制器类等步骤,并探讨了不同组件加载方式及自定义启动画面的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot学习笔记

1.创建pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.spring.boot</groupId>
    <artifactId>BootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 引入Spring Boot支持 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Spring Boot 打包器 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 第一个SpringBoot程序

// 启动类
@Configuration
@EnableAutoConfiguration
public class SpringBootStarter {

    public static void main(String[] args) throws Exception {
        Object[] resources = {SpringBootStarter.class,UserController.class};
        SpringApplication.run(resources, args);
    }
}

// SpringBoot中的一个controller
@Controller
public class UserController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping("/")
    @ResponseBody
    String home() {
        logger.info("hello spring boot");
        return "Hello World!";
    }
}
2.1 @RestController and @RequestMapping

@RestController and @RequestMapping 注解都属于SpringMVC的注解

@RestController 是标识当前Controller类输入RestFul形式,请求后返回值会按照json格式返回

@RequestMapping 是指定http请求访问路径

eg.

@RestController
public class RsController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello spring boot";
    }
}
2.2 @EnableAutoConfiguration

这个注解告诉SpringBoot去“猜”我们如何配置Spring,这个要建立在添加的jar包,

3.构建代码

3.1 使用 default package

正常来说,在我们的代码结构中,尽量使用域名倒置,并且按照模块进行分包,不建议直接将类放到src根目录下。 eg.我们的域名是geek.com ,建议 包命名为 com.geek。

3.2 项目结构
com
 +- example
     +- myproject
         +- Application.java
         |
         +- domain
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

官方文档中说在Application.java的位置,并且配置成@SpringBootApplication或者 @ComponentScan,可以扫描所有目录

但是个人感觉,如果扫描所有类必然会导致,加载速度过慢,所以建议使用@ComponentScan并指定扫描的目录 eg.

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.geek.controller,com.geek.service")
public class SpringBootStarter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootStarter.class, args);
    }
}

4.加载类

4.1 使用@ComponentScan加载
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.geek.controller,com.geek.service")
public class SpringBootStarter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootStarter.class, args);
    }
}
4.2 使用@Import加载
@Configuration
@EnableAutoConfiguration
@Import(UserController.class)
public class SpringBootStarter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootStarter.class, args);
    }
}

这种方式不太好用,如果类比较多,那就要写特别多的class

4.3 使用@ImportResource加载xml方式
@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:beans.xml")
public class SpringBootStarter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootStarter.class, args);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
	<bean class="com.geek.controller.UserController"></bean>

</beans>

官方件建议使用注解的方式加载

5.自定义banner

在SpringBoot启动时会出现下面的画面

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::   v1.3.6.RELEASE

看起来很6的样子,其实SpringBoot为我们提供了自定的banner显示

5.1自定义banner
  1. 首先我们要在resource目录下添加一个banner.txt
  2. 在文档中输入显示的字符画即可
5.2 关闭banner
public static void main(String[] args) {

	    SpringApplication app = new SpringApplication(MySpringConfiguration.class);
	    app.setBannerMode(Banner.Mode.OFF);
	    app.run(args);
	}

转载于:https://my.oschina.net/ifaxin/blog/719726

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值