springboot---基本模块详解

本文详述了SpringBoot的各个核心模块,包括spring-boot-starter-jdbc、spring-boot-starter-web、spring-boot-starter-test、spring-boot-devtools、spring-boot-starter-aop和spring-boot-starter-security。讲解了每个模块的功能、自动配置原理以及如何定制配置。还介绍了mybatis-spring-boot-starter、pagehelper-spring-boot-starter、druid-spring-boot-starter的使用,以及mybatis-generator-core和commons-lang3这两个工具库的作用。

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

概述

1.基于Spring框架的“约定优先于配置(COC)”理念以及最佳实践之路。
2.针对日常企业应用研发各种场景的Spring-boot-starter自动配置依赖模块,且“开箱即用”(约定spring-boot-starter- 作为命名前缀,都位于org.springframenwork.boot包或者命名空间下)。

spring-boot-starter-jdbc

默认情况下,当我们没有配置任何DataSource,SpringBoot会为我们自动配置一个DataSource,这种自动配置的方式一般适用于测试,开发还是自己配置一个DataSource的实例比较好。
如果我们的工程只依赖一个数据库,那么,使用DataSource自动配置模块提供的参数是最方便的:

spring.datasource.url=jdbc:mysql://{datasource host}:3306/{databaseName}
spring.datasource.username={database username}
spring.datasource.passwd={database passwd}

还会自动配置的有:JdbcTemplate DateSourceTransactionManager等,我们只要在使用的时候注入(@Autowired)就好了
此外,SpringBoot还支持的数据库有spring-boot-data-jpa spring-boot-data-mongodb

spring-boot-starter-web

在当下项目运行mvn spring-boot:run就可以直接启用一个嵌套了tomcat的web应用。
如果没有提供任何服务的Cotroller,访问任何路径都会返回一个springBoot默认的错误页面(Whitelabel error page)。
嵌入式Web容器层面的约定和定制

spring-boot-starter-web默认使用嵌套式的Tomcat作为Web容器对外提供HTTP服务,默认端口8080对外监听和提供服务。
我们同样可以使用spring-boot-starter-jetty或者spring-boot-starter-undertow作为Web容器。
想改变默认的配置端口,可以在application.properties中指定:

server.port = 9000(the port number you want)

类似的配置还有:

server.address
server.ssl.*
server.tomcat.*

如果上诉仍然没有办法满足要求,springBoot支持对嵌入式的Web容器实例进行定制,可以通过向IoC容器中注册一个EmbeddedServletContainerCustomizer类型的组件来对嵌入式的Web容器进行定制

public class UnveilSpringEmbeddedTomcatCustomizer implements EmbeddedServletContainer{
   
   
        public void customize(ConfigurableEmbeddedServletContainer container){
   
   
            container.setPort(9999);
            container.setContextPath("C\\hello");
                           ...
        }
    }

spring-boot-starter-test

进行springboot单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
   
   

	@Autowired
	private WebApplicationContext wac;

	private MockMvc mockMvc;

	@Before
	public void setup() {
   
   
		mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
	}

	@Test
	public void whenQuerySuccess() throws Exception {
   
   
		String result = mockMvc.perform(
				get("/user").param("username", "jojo").param("age", "18").param("ageTo", "60").param("xxx", "yyy")
						// .param("size", "15")    //MockMvcRequestBuilders
						// .param("page", "3")
						// .param("sort", "age,desc")
						.contentType(MediaType.APPLICATION_JSON_UTF8))
				.andExpect(status().isOk()).andExpect(jsonPath("$.length()").value(3)) //MockMvcResultMatchers
				.andReturn().getResponse().getContentAsString();
		
		System.out
<think>好的,我现在需要帮助用户了解在Spring Boot中配置Druid数据源以及启动流程的详细信息。首先,我得回想一下Druid和Spring Boot整合的相关知识。Druid是一个数据库连接池,提供了监控和扩展功能,而Spring Boot Starter简化了配置步骤。 用户的需求是详细的配置步骤和启动流程,所以我需要分步骤说明。首先,应该从项目依赖开始,用户需要添加Druid Spring Boot Starter的依赖。接着是配置文件,比如application.yml中的设置,包括数据库URL、用户名、密码、连接池参数等。这部分需要具体示例,比如展示如何配置initialSize、minIdle、maxActive等参数,同时还要提到如何启用监控功能,比如配置StatViewServlet和WebStatFilter。 然后,启动流程方面,需要解释Spring Boot的自动配置机制,DruidDataSourceAutoConfigure如何初始化数据源,并绑定配置属性。这里可能需要提到@ConfigurationProperties的作用,以及如何通过配置文件注入属性。同时,监控功能的初始化过程,比如Servlet和Filter的注册,也是关键点。 用户提供的引用中,引用[1]和[2]提到了Druid Spring Boot Starter的项目地址和安装配置,引用[3]涉及多数据源配置。虽然用户的问题没有明确提到多数据源,但或许在相关问题时可以提及,但当前问题应集中在单数据源的配置和启动流程。 接下来,我需要确保回答结构清晰,分点说明配置步骤和启动流程。同时,按照用户要求,生成相关问题,比如如何配置多数据源、如何监控SQL性能等。这些相关问题需要基于当前主题自然延伸,帮助用户深入了解。 还要注意使用中文,行内公式用$...$,独立公式用$$...$$,但在这个问题中可能数学公式较少,主要关注代码块和配置示例。确保LaTeX语法正确,比如在提到变量时正确使用格式。 最后,检查是否引用了用户提供的引用,特别是在提到监控配置时,可能需要引用[1]和[2]中的内容。确保回答真实可靠,步骤准确,示例代码正确,并且语言流畅,符合用户的要求。</think>### Druid Spring Boot 数据源配置详解 #### 一、依赖配置 在`pom.xml`中添加 Druid Spring Boot Starter 依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> <!-- 版本号根据实际情况调整 --> </dependency> ``` 该依赖会自动引入 Druid 核心库和 Spring Boot 集成模块[^1]。 #### 二、基础数据源配置 在`application.yml`中配置数据源参数: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 validation-query: SELECT 1 test-on-borrow: false test-on-return: false test-while-idle: true ``` - `initial-size`: 初始化连接数 - `max-active`: 最大活跃连接数 - `test-while-idle`: 空闲时检测连接有效性 #### 三、监控功能配置 ```yaml spring: datasource: druid: stat-view-servlet: enabled: true url-pattern: /druid/* login-username: admin login-password: admin web-stat-filter: enabled: true url-pattern: /* exclusions: "*.js,*.gif,*.jpg,/druid/*" ``` - `stat-view-servlet`: 启用监控页面 - `web-stat-filter`: 统计 Web 请求 #### 四、启动流程解析 1. **自动配置阶段** Spring Boot 通过`DruidDataSourceAutoConfigure`自动创建数据源实例,绑定`spring.datasource.druid`前缀的配置参数[^2]。 2. **连接池初始化** $$DruidDataSource \rightarrow init() \rightarrow create \ physical \ connections$$ 根据`initial-size`创建初始连接,建立连接池基础架构。 3. **监控初始化** 通过`ServletRegistrationBean`注册`StatViewServlet`,通过`FilterRegistrationBean`注册`WebStatFilter`,实现监控功能[^1]。 4. **健康检查集成** Spring Boot Actuator 自动集成 Druid 的健康检查端点,可通过`/actuator/health`查看数据库状态。 #### 五、验证配置 1. 访问监控界面:`http://localhost:8080/druid` 2. 检查日志输出: ``` 2023-xx-xx INFO o.s.b.a.d.DruidDataSourceAutoConfigure : Init DruidDataSource 2023-xx-xx INFO com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值