依赖管理
什么是依赖管理
- spring-boot-starter-parent 还有父项目,声明了开发中常用的依赖的版本号。
- 并且进行自动版本仲裁 ,即如果程序员没有指定某个依赖 jar 的版本,则以父项目指定的版本为准。

2023年11月17日大约 56 分钟
官网:https://spring.io/projects/spring-boot
学习文档:https://docs.spring.io/spring-boot/docs/current/reference/html/
在线 API:https://docs.spring.io/spring-boot/docs/current/api/
Spring Boot 可以轻松创建独立的、生产级的基于 Spring 的应用程序。
Spring Boot 直接嵌入 Tomcat、Jetty 或 Undertow ,可以"直接运行" Spring Boot 应用程序。
@Component、@Controller、 @Service、@Repository
这些在 Spring 中的传统注解仍然有效,通过这些注解可以给容器注入组件。
通过 @Configuration 创建配置类来注入组件。
创建 src/main/java/com/lzw/springboot/config/BeanConfig.java
package com.lzw.springboot.config;
import com.lzw.springboot.bean.Monster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author LiAng
* 1. @Configuration 标识这是一个配置类, 等价于配置文件
* 2. 程序员可以通过@Bean 注解注入bean对象到容器
* 3. 当一个类被 @Configuration 标识,该类-Bean 也会注入容器
*/
@Configuration
public class BeanConfig {
/**
* 1. @Bean : 给容器添加组件, 就是Monster bean
* 2. monster01() : 默认 你的方法名monster01 作为Bean的名字/id
* 3. Monster : 注入类型, 注入bean的类型是Monster
* 4. new Monster(200,"牛魔王",500,"疯魔拳") 注入到容器中具体的Bean信息
* 5. @Bean(name = "monster_nmw") : 在配置、注入Bean指定名字/id monster_nmw
* 6. 默认是单例注入
* 7. 通过 @Scope("prototype") 可以每次返回新的对象,就多例.
* @return
*/
//@Bean(name = "monster_nmw")
@Bean
//@Scope("prototype")
public Monster monster01(){
return new Monster(200, "牛魔王", 500, "疯魔拳");
}
}
1、创建 Maven 项目 lzw-springboot
2、导入相关依赖
<?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>com.lzw</groupId>
<artifactId>lzw-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入springboot父工程-规定写法-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
SpringBoot 和 IDEA 官方支持