1.Springboot入门
-
创建Maven工程
-
导入spring-boot-stater-web起步依赖
-
编写Controller
-
提供启动类
2.yml配置信息书写与获取
书写
# 发件人信息 email:user: 172349823457@qq.comcode: sajdajlwhjfgfkllwhost: smtp.qq.comauth: true # 学生爱好 hobbies:- 打篮球- 踢足球- 打游戏
获取
@Value("${mail.user}")
更便利的方法:
# 要求实体类的成员变量名与配置文件中的键名保持一致 @ConfigurationProperties(prefix = "email")
3.bean扫描
// 启动类 @SpringBootApplication public class SpringBootQuickstartApplication{public static void main(String[] args){SpringApplication.run(SpringbootQuickstartApplication.class,args);} } //其中@SpringBootApplication包含了以下内容 @SpringBiitConfiguration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication{ } //也可以通过@ComponentScan(basePackages="com.example")来扫描包
4.bean注册
如果要注册的bean对象来自第三方,是无法用@Component及其衍生注解声明bean 的
//通过@Bean //直接在启动类中,编写 //注入Country对象 @Bean //将方法返回值交给IOC容器管理,成为IOC容器的bean对象 public Country country(){return new Country(); } //通过@Import //1.导入配置类 @Import(CommonConfig.class) @SpringBootApplication public class SpringBootApplication{ } //2.导入ImportSelector接口实现类 public class CommonImportSelector Implements ImportSelector{@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata){return new String[]("com.example.config.CommonConfig");} } public class CommonImportSelector implements ImportSelector {@Ovrridepublic String[] selectImports(AnnotationMetadata importingClassMetadata){// 读取配置文件的内容List<String> imports = new ArrayList<>();InputStream is = CommonImportSelector.class.getClassLoader().getResourceAsStream("common.imports");BufferedReader br = new BufferedReader(new InputStreamReader(is));String line = null;try {while((line = br.readLine())!=null){imports.add(line);}} catch (IOException e) {throw new RuntimeException(e);} finally {if (br!=null){try {br.close();}catch (IOException e) {throw new RuntimeException(e);}}}return imports.toArray(new String[0]);} }
5.注册条件
SpringBoot提供了设置注册生效条件的注解@Conditional
//若配置文件中配置了指定的信息,则注入,否则不注入 @ConditionalOnProperty(prefix = "country",name = {"name","system"}) @Bean public Country country(@Value("${country.name}") String name,@Value("${country.system}") String system){Country country = new Country();country.setName(name);country.setSystem(system);return country; } //若IOC容器中不存在Country,则注入Province,否则不注入 @Bean @ConditionalOnMissingBean(Country.class) public Province province(){return new Province(); } //若当前环境中存在DispatcherServlet类,则注入Province,否则不注入 //若当前引入了web起步依赖,则环境中有DispatcherServlet,否则没有 @Bean @ConditionalOnClass(name = "org.springframework.web.servlet.DispatcherServlet") public Province province(){return new Province(); }
6.自动配置原理
遵循约定大于配置的原则,在boot程序启动后,起步依赖中的一些bean对象会自动注入到ioc容器中
7.自定义starter
@AutoConfiguration //表示当前类是一个自动配置类 public class MyBatisAutoConfig {//SqlSessionFactoryBean@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){SqlSessionFactoryBean sqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean;}//MapperScannerConfigure@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(BeanFactory beanFactory){MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();//扫描的包:启动类所在的包及其子包List<String> packages = AutoConfigurationPackages.get(beanFactory);String p = packages.get(0);mapperScannerConfigurer.setBasePackage(p);//扫描的注解mapperScannerConfigurer.setAnnotationClass(Mapper.class);return mapperScannerConfigurer;} }
步骤
-
创建 dmybatis-spring-boot-autoconfigure 模块,提供自动配置功能,并自定义配置文件 META-INF/spring/xxx.imports
-
创建 dmybatis-spring-boot-starter 模块,在starter中引入自动配置模块