Springboot是Spring的一个子项目,用于快速构建Spring应用程序
①创建SpringBoot工程
②编写Controller
@RestController
public class HelloContoller {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
③运行启动类:xxxxApplication
用于修改SpringBoot相关配置
Spring提供了多种配置文件格式:只有格式区别
1、application.properties
2、application.yml(更常用)
配置信息的书写和使用:
email:
user: wmh031024@163.com
code: xxxxxxx
① @Value
@Component
public class EmailProperties {
@Value("${email.user}")
public String user;
@Value("${email.code}")
public String code;
}
② @ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "emil")
public class EmailProperties {
public String user;
public String code;
}
1、导入maven坐标
<!--MySQL驱动依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、配置文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/Spring
username: root
password: root
3、Mapper类
@Mapper
public interface UserMapper {
@Select("select * from class1 where id = #{id}")
public User findById(Integer id);
}
注册第三方的Bean对象时,没法办用@Component
注解声明Bean。
1、创建配置类 ,使用 @Bean
注入对象
@Configuration
public class CommonConfig {
@Bean
public User user() {
return new User();
}
}
2、导入配置类
方式一(单个导入): 使用@Import
直接导入
@SpringBootApplication
@Import(CommonConfig.class)
public class BootDemo1Application {
public static void main(String[] args) {
SpringApplication.run(BootDemo1Application.class, args);
}
}
**方式二(多个导入):**使用@Import
导入 ImportSelector
接口实现类
public class CommonImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"cn.wmhwiki.config.CommonConfig"};
}
}
@SpringBootApplication
@Import(CommonImportSelector.class)
public class BootDemo1Application {
public static void main(String[] args) {
SpringApplication.run(BootDemo1Application.class, args);
}
}
Bean注册并相应的值
user:
name: yx
value: 250
@Bean
public User user(@Value("${user.name}") String name, @Value("${user.value}") String value) {
return new User(name, value);
}
SpringBoot提供了设置注册生效条件的注解 @Conditional
注解 | 说明 |
---|---|
@ConditionalOnProperty | 配置文件中存在对应的属性,才声明该bean |
@ConditionalOnMissingBean | 当不存在当前类型的bean时,才声明该bean |
@ConditionalOnClass | 当前环境存在指定的这个类时,才声明该bean |
在实际开发中,经常会定义一些公共组件,提供给各个项目团队使用。而在SpringBoot的项目中,一般会将这些公共组件封装为SpringBoot 的 starter。
- 创建 dmybatis-spring-boot-autoconfigure 模块,提供自动配置功能,并自定义配置文件 META-INF/spring/xxx.imports
- 创建 dmybatis-spring-boot-starter 模块,在starter中引入自动配置模块
以Mybatis为例,制作Mybatis自动配置
1、创建dmybatis-spring-boot-autoconfig
2、导入maven坐标
照着mybatis-spring-boot-starter导包
3、编写MyBatisAutoConfig配置类
@AutoConfiguration
public class MyBatisAutoConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(BeanFactory beanFactory) {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// 包扫描
List<String> packages = AutoConfigurationPackages.get(beanFactory);
mapperScannerConfigurer.setBasePackage(packages.get(0));
// 注解扫描
mapperScannerConfigurer.setAnnotationClass(Mapper.class);
return mapperScannerConfigurer;
}
}
4、创建imports文件,填入配置类的全类名
META-INF/spring/org.springframework.boot.test.autoconfigure.core.AutoConfigureCache.imports
5、创建dmybatis-spring-boot-starter
6、导入maven坐标
7、导入jdk17插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
8、测试使用