java篇-springboot中的starter

Spring Boot中的starter是什么?有什么优点?

在SpringBoot中,Starter是一种Maven依赖项集合,可以方便地引入一组相关的依赖项来支持某种特定的功能。

SpringBootStarter命名规则通常是spring-boot-starter-*,其中 * 表示某种包括技术,模块或服务等

例如:spring-boot-starter-web就是一个web应用程序所需要的基本依赖项集合,包含了SpringMVC,Tomcat和其他依赖项。

SpringBoot 常用的 Starter 有哪些?

spring-boot-starter-web :提供 Spring MVC + 内嵌的 Tomcat 。
spring-boot-starter-data-jpa :提供 Spring JPA + Hibernate 。
spring-boot-starter-data-Redis :提供 Redis 。
mybatis-spring-boot-starter :提供 MyBatis 。


Spring Boot 的核心注解是哪个?

启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:

1、@SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。

2、@EnableAutoConfiguration:打开自动配置的功能。

     也可以关闭某个自动配置的选项,如关闭数据源自动配置功能:@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。

3、@ComponentScan:Spring组件扫描。

springboot自动配置原理

@SpringBootApplication:是@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan的组合。

1、@Configuration表示该类是Java配置类。

2、@ComponentScan开启自动扫描符合条件的bean(添加了@Controller、@Service等注解)。

3、@EnableAutoConfiguration会根据类路径中的jar依赖为项目进行自动配置,比如添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的依赖,然后Spring Boot会对Tomcat和Spring MVC进行自动配置

SpringBootStarter优点

简化应用程序的构建:Starter可以帮助开发人员快速引入所需的依赖项,从而简化构建过程
确保依赖项版本兼容性:Starter可以确保所有的依赖项版本兼容性,避免因版本不兼容而导致的依赖项冲突问题。
避免不必要的依赖项:Starter只包含必要的依赖项,避免了不必要的依赖项的引入,从而减少了应用程序的体积。
可扩展性强:在需要自定义配置或添加其他依赖项时,可以通过继承和覆盖Starter中的配置文件或类来扩展Starter。

Spring Boot项目打的jar包和普通的jar有什么区别?

SpringBoot打的jar包内置了Tomcat,Jetty等Web服务器,可以直接通过命令java -jar xxx.jar启动应用程序,不需要手动部署到Web服务器中。这种方式使得应用程序的部署和运维非常方便。

这种jar不可以作为普通的jar被其他项目依赖,即使依赖也无法使用其中的类。无法被其他项目依赖主要原因是他和普通jar结构不同。

普通的jar包,解压后就是包名,包里面就是代码,而SpringBoot 的jar包解压后在\Boot-INF\classes目录下才是我们的代码,因此无法直接引用。如果非要引用,可以在pom.xml文件增加配置,将项目打成两个jar,一个可执行,一个可引用。

  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将一个druid-springboot-starterspringboot项目转换成使用shardingjdbc的springboot项目的步骤如下: 1. 在pom.xml文件增加sharding-jdbc-spring-boot-starter和mysql-connector-java的依赖。 ```xml <dependency> <groupId>io.shardingjdbc</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${shardingjdbc.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> ``` 2. 在application.yml或application.properties配置sharding-jdbc的数据源和分库分表规则。 ```yaml spring: shardingsphere: datasource: names: ds0, ds1 # 数据源名称 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root sharding: tables: t_order: actual-data-nodes: ds$->{0..1}.t_order_$->{0..1} table-strategy: inline: sharding-column: order_id algorithm-expression: t_order_$->{order_id % 2} key-generator: type: SNOWFLAKE column: order_id binding-tables: t_order default-database-strategy: inline: sharding-column: user_id algorithm-expression: ds$->{user_id % 2} default-table-strategy: none: ``` 3. 在代码使用sharding-jdbc的数据源访问数据库。 ```java @Autowired private JdbcTemplate jdbcTemplate; public void query() { String sql = "select * from t_order where user_id = ?"; List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, 10); System.out.println(resultList); } ``` 通过以上步骤,就可以将一个druid-springboot-starterspringboot项目转换成使用shardingjdbc的springboot项目了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值